Search code examples
ruby-on-railsruby-on-rails-4redmineredmine-plugins

How to add history item from Redmine plugin using journal


I have update the meeting plugin. in this plugin when update the meeting it doesnot update the meeting history. like i change the subject for meeting it update the subject but dont update the history like issue.

my configuration of redmine is as following

Environment:
  Redmine version                3.3.1.stable
  Ruby version                   2.1.5-p273 (2014-11-13) [x86_64-linux]
  Rails version                  4.2.7.1
  Environment                    development
  Database adapter               Mysql2

I have done following->

for my model class

 def init_journal(user, notes = "")
  @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes, :notify => false)

    if new_record?
      @current_journal.notify = false
    else
      @attributes_before_change = attributes.dup
    end
    @current_journal
  end

  def last_journal_id
    if new_record?
      nil
    else
      journals.maximum(:id)
    end
  end

  # Returns a scope for journals that have an id greater than journal_id
  def journals_after(journal_id)
    scope = journals.reorder("#{Journal.table_name}.id ASC")
    if journal_id.present?
      scope = scope.where("#{Journal.table_name}.id > ?", journal_id.to_i)
    end
    scope
  end

  def create_journal
    if @current_journal
      # attributes changes
      if @attributes_before_change
        (Meeting.column_names - %w(id created_at updated_at)).each {|c|
          before = @attributes_before_change[c]
          after = send(c)
          next if before == after || (before.blank? && after.blank?)
          @current_journal.details << JournalDetail.new(:property => 'attr',
                                                        :prop_key => c,
                                                        :old_value => before,
                                                        :value => after)
        }
      end
      @current_journal.save
      # reset current journal
      init_journal @current_journal.user, @current_journal.notes
    end
  end

and for controller class

def update_meeting_from_params
    @edit_allowed = User.current.allowed_to?(:edit_meetings, @project)
    @time_entry = TimeEntry.new(:meeting => @meeting, :project => @meeting.project)
    @time_entry.attributes = params[:time_entry]

    @meeting.init_journal(User.current)

    meeting_attributes = params[:meeting]
    @meeting.safe_attributes = meeting_attributes
  end

I get following error. http://prntscr.com/eb257g please help me


Solution

  • Your journalized object (Meeting) should have a method called journalized_attribute_names

    Try to add it to your model (meeting.rb):

    # Returns the names of attributes that are journalized when updating the meeting
    def journalized_attribute_names
      names = Meeting.column_names - %w(id created_on updated_on)
      names
    end