global methods in ruby on rails models

前端 未结 3 1004
孤城傲影
孤城傲影 2021-02-04 18:33

I have methods in all of my models that look like this:

def formatted_start_date
  start_date ? start_date.to_s(:date) : nil
end

I would like

相关标签:
3条回答
  • 2021-02-04 19:05

    An answer to your comment: You can extract common code/functionality into modules which you include in a class (I belive that is called mixin?) or you can go for subclasses. I don't think subclass is the way to go when its just common functionality you want to get into your objects, and not a real inheritance situation.

    Take a look at this for more info on modules and Ruby.

    0 讨论(0)
  • 2021-02-04 19:17

    It looks more like something for a helper to me. Try this in the your application help:

    def formatted_date(date)
       date ? date.to_s(:date) : nil
    end
    

    Formatting isn't something that really belongs in the model (for precisely the reason you've discovered... putting such common code in every model is annoying)

    If you really want to do as you say though, then what you could do is monkeypatch the ActiveRecord superclass and add the function you want into there. It would then be available for all your models. Beware that monkeypatching can lead to unpredictable and undefined behaviour and use at your own risk! It's also pretty hacky :)

    class ActiveRecord::Base
       def formatted_start_date
          start_date ? start_date.to_s(:date) : nil
       end
    end
    

    Just stick that somewhere that will be run before anything else in your app will be, and it will dynamically add the method to the base class of your models, making it available for use.

    Or you could create a mixin for all your models, but that seems a bit overkill for a single method.

    0 讨论(0)
  • 2021-02-04 19:28

    I just had to answer this, cos it's a fun Ruby excercise.

    Adding methods to a class can be done many ways, but one of the neatest ways is to use some of the reflection and evaluation features of Ruby.

    Create this file in your lib folder as lib/date_methods.rb

    module DateMethods
    
      def self.included(klass)
    
        # get all dates
        # Loop through the class's column names
        # then determine if each column is of the :date type.
        fields = klass.column_names.select do |k| 
                      klass.columns_hash[k].type == :date
                    end
    
    
        # for each of the fields we'll use class_eval to
        # define the methods.
        fields.each do |field|
          klass.class_eval <<-EOF
            def formatted_#{field}
              #{field} ? #{field}.to_s(:date) : nil
            end
    
          EOF
        end
      end
    end
    

    Now just include it into any models that need it

     class CourseSection < ActiveRecord::Base
       include DateMethods
     end
    

    When included, the module will look at any date columns and generate the formatted_ methods for you.

    Learn how this Ruby stuff works. It's a lot of fun.

    That said, you have to ask yourself if this is necessary. I don't think it is personally, but again, it was fun to write.

    -b-

    0 讨论(0)
提交回复
热议问题