lundi 26 octobre 2015

Does it belong to controller or model?

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 ?

Aucun commentaire:

Enregistrer un commentaire