2012-11-08 107 views
0

我有3種型號:添加自類模型軌道3

Class Model1 

end 

Class Model2 

end 

Class Model3 

end 

我有這樣的代碼:

scope :created_between, lambda { |start_time, end_time| where(:created_at => (start_time...end_time)) } 

class << self 
## Class methods for calculating searches 
def created_today 
    today = Time.zone.now 
    created_between(today.beginning_of_day, today.end_of_day) 
end 

def created_yesterday 
    yesterday = Time.zone.now - 1.day 
    created_between(yesterday.beginning_of_day, yesterday.end_of_day) 
end 

def created_last_week 
    start_time = (Time.zone.now - 1.week).beginning_of_day 
    end_time = Time.zone.now 
    created_between(start_time, end_time) 
end 

def created_last_month 
    start_time = (Time.zone.now - 1.month).beginning_of_day 
    end_time = Time.zone.now 
    created_between(start_time, end_time) 
end 

def created_last_year 
    start_time = (Time.zone.now - 1.year).beginning_of_day 
    end_time = Time.zone.now 
    created_between(start_time, end_time) 
end 

我需要這個代碼添加到3款,但我不不想在每個模型中重複代碼。

我該怎麼辦?

非常感謝!

+1

我不會爲第一種方法使用範圍。你可以用另一個帶有兩個參數的類方法來實現,然後你不需要lamba。 –

+0

謝謝,你能添加一個例子嗎? – hyperrjas

+0

只要完成你對其他方法的操作,但是該方法需要兩個參數。 –

回答

6

試試這個:

module CommonClassFunctions 

def self.included(base) 
    base.send :extend, ClassMethods 
end 

module ClassMethods 
    ## Class methods for calculating searches 
    def created_today 
    today = Time.zone.now 
    created_between(today.beginning_of_day, today.end_of_day) 
    end 

    def created_yesterday 
    yesterday = Time.zone.now - 1.day 
    created_between(yesterday.beginning_of_day, yesterday.end_of_day) 
    end 

    def created_last_week 
    start_time = (Time.zone.now - 1.week).beginning_of_day 
    end_time = Time.zone.now 
    created_between(start_time, end_time) 
    end 

    def created_last_month 
    start_time = (Time.zone.now - 1.month).beginning_of_day 
    end_time = Time.zone.now 
    created_between(start_time, end_time) 
    end 

    def created_last_year 
    start_time = (Time.zone.now - 1.year).beginning_of_day 
    end_time = Time.zone.now 
    created_between(start_time, end_time) 
    end 
    end 
end 

Class Model1 
    include CommonClassFunctions 
end 

Class Model2 
    include CommonClassFunctions 
end 

Class Model3 
    include CommonClassFunctions 
end 
+0

非常感謝你完成這個例子。 – hyperrjas

+0

好的答案:),爲什麼不是'AS :: Concern'?它更簡單,並提供更多的功能。我可以編輯使用「ActiveSupport :: Concern」的答案嗎? – rewritten

3

您可以使用此方法定義模塊,然後將其包含在3個模型中。

像:

module Searcheable 
    def created_today 
    . 
    . 
    . 
    . 
    . 
    end 
end 

然後在類:

class Model1 
    extend Searcheable 
end 

class Model2 
    extend Searcheable 
end 

class Model3 
    extend Searcheable 
end 
+0

謝謝你在我的rails項目的文件夾中創建文件的模塊? – hyperrjas

+0

該模塊可以放在lib文件夾中。 –

+0

好(可能更好)答案 - 組成通常比繼承更好!儘管它仍然依賴於用例... – awenkhh

0

你可以繼承做到這一點

class Model 
    class << self 
    def created_today 
     puts "created today from Model" 
    end 
    end 
end 

class Model1 < Model 

end 

class Model2 < Model 

end 

class Model3 < Model 

end 

Model1.created_today 
# output: created today from Model