Search code examples
ruby-on-rails-3redmineredmine-plugins

Redmine: extend controller action in plugin


Need some help in plugin development. I've created hook in user/edit form view, added ballance_amount to form and have "ballance_amount"=>"1".

How can I extend default update action in user controller?

In base.class_eval do I've added alias_method_chain :update, :ballance In InstanceMethods:

def update_with_ballance
    ballance.amount = params[:user][:ballance_amount].to_f #I have ballance association
end

And get this:

NameError (undefined local variable or method `params' for #<User:0x007f972e9379d0>):
app/controllers/users_controller.rb:144:in `update'

How can I fetch params?


Solution

  • You should be able to make use of the mass-assignment code in the Redmine itself. Line 135 in the UsersController should handle provide you with a simple entry point for your extension, if balance_amount is considered a safe_attribute. To achieve that, add a patch like the following to the User model:

    module RedmineBalancePlugin::UserPatch
      def self.included(base)
        base.class_eval do
          safe_attributes 'balance_amount'
    
          include InstanceMethods
        end
      end
    
      module InstanceMethods
        # This method is indirectly called when creating or updating a User
        def balance_amount=(amount)
          balance.amount = amount
        end
    
        # This could be useful in your view patch, but maybe not
        def balance_amount
          balance.amount
        end
      end
    end
    
    User.send(:include, RedmineBalancePlugin::UserPatch)
    

    If this example does not help, it would be helpful, if you could provide more code fragments - e.g. the complete patch to the UsersController, to make it easier to reproduce and analyse the issue.