In order to create new models or customize existing ones, you can use the app/models/custom/ folder.
If you're adding a new model that doesn't exist in the original Consul Democracy, simply add it to that folder.
If, on the other hand, you are changing a model that exists in the application, create a file in app/models/custom/ with same name as the file you're changing. For example, if you're changing the app/models/budget/investment.rb file, create a file named app/models/custom/budget/investment.rb and require the original one:
With this code, the custom model will have all the methods of the original model (because it loads the original file) plus the last_month scope.
Modifying existing methods
When modifying existing methods, it is strongly recommended that, when possible, your custom code calls the original code and only modifies the cases where it should behave differently. This way, when upgrading to a new version of Consul Democracy that updates the original methods, your custom methods will automatically include the modifications in the original code as well. Sometimes this won't be possible, though, which means you might need to change your custom method when upgrading.
For example, to change the common abilities model so only verified users can create comments, we'll create a file app/models/custom/abilities/common.rb with the following content:
You can find another example in the app/models/custom/setting.rb file.
load Rails.root.join("app", "models", "abilities", "common.rb")
module Abilities
class Common
alias_method :consul_initialize, :initialize # create a copy of the original method
def initialize(user)
consul_initialize(user) # call the original method
cannot :create, Comment # undo the permission added in the original method
if user.level_two_or_three_verified?
can :create, Comment
end
end
end
end