Reputation: 1502
I have this before_action
method :
def convert_duration_reminder
allowed_duration_units = %(hour day)
if allowed_duration_units.include? params[:store][:store_setting_attributes][:reminder_unit]
store_settings_params = params[:store][:store_setting_attributes]
@duration_reminder = store_settings_params[:reminder_duration].to_i.send(store_settings_params[:reminder_unit]).to_i / 3600
else
redirect_to store_settings_store_path(id: current_store.id)
end
end
I have a little bit of logic inside. The @duration_reminder
is after used in an update_attributes
.
Does it belongs to controller ? For me yes because I'm using params and I have a redirect and methods are short (except line length...) and doesn't process any data in database.
This method simply convert times in minutes according to what the user choose in the view ("2 hours", "1 day").
From the book Rails 4 action, it's write :
■ Models contain the domain logic of your application. This logic dictates how the records in your database are retrieved, validated, or manipulated. In Rails appli- cations, models define the code that interacts with the database’s tables to retrieve and set information in them. Domain logic also includes things such as validations or particular actions to be performed on the data.
■ Controllers interact with the models to gather information to send to the view. They’re the layer between the user and the database. They call methods on the model classes, which can return single objects representing rows in the data- base or collections (arrays) of these objects. Controllers then make these objects available to the view through instance variables. Controllers are also used for permission checking, such as ensuring that only users who have special permission to perform certain actions can perform those actions, and users without that permission can’t.
Does def convert_duration_reminder
belongs to controller ?
Upvotes: 0
Views: 1127
Reputation: 76784
A simple test (of whether it's a controller / model action) is to look at what the action is meant to do - does it work with data or params?
Rails models are meant to be "locked away" from much of the higher-level logic in your application; it's meant to provide a base-level set of actions to manipulate the data being inserted into your db; anything requiring request-level data should be handled in the controller.
As such...
redirect_to store_settings_store_path(id: current_store.id)
... because you're manipulating the flow of your app, your logic should stay in the controller.
If you weren't redirecting (IE were only manipulating data), you'd be able to put this functionality into a before_create
callback in your model.
Upvotes: 4