Is the ActionText call form nice in the model?

Is it permissible to call ActionText from ActiveRecord?

def last_updated
  updated_at? ? "Updated #{ActionText::time_ago_in_words(updated_at)} ago" : "never updated"
end

Is there an argument against doing something like this? Should this be a helper method?

+3
source share
2 answers

Yes. According to strict MVC teachings, this is a bad form and belongs to an assistant.

In theory: this particular piece of functionality is presentation-specific, so, according to the teachings of MVC, it refers to a view helper.

In practice: it really doesn't matter. I believe ActionText is parsed when loading the server / console. This means that the file does not need to be downloaded again. Also, calling a method directly rather than enabling an ActionText is probably better on the part of memory management.

- .

, (model.last_updated) , (last_updated(model)). . , . , - .

, :

module ModelHelper 
  def included(base)
    Model.send(:include, ModelInstanceMethods)
  end

  module ModelInstanceMethods
    def last_updated
      updated_at? ? "Updated #{ActionText::time_ago_in_words(updated_at)} ago" : "never updated"
    end
  end
end

Model - , .

+1

, . , , .

0

Source: https://habr.com/ru/post/1733070/


All Articles