Module: Railstar::SearchBase

Defined in:
lib/railstar/search_base.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/railstar/search_base.rb', line 18

def self.included base
  base.class_eval do
    attr_accessor :where, :values
    extend ClassMethods
    include ActiveModel::Naming
    include ActiveModel::Validations
    include ActiveModel::Conversion
  end
end

Instance Method Details

#count(options = {}) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/railstar/search_base.rb', line 72

def count(options={})
  reset_conditions
  create_conditions
  target_model.count("#{target_model.table_name}.id", options.merge(
                                                                    :conditions => where.blank? ? nil : [where.join(" and "), values]
                                                                    ))
end

#find(options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/railstar/search_base.rb', line 62

def find(options={})
  reset_conditions
  create_conditions
  if where.blank?
    target_model.scoped
  else
    target_model.where([where.join(" and "), values])
  end
end

#initialize(conditions = {}) ⇒ Object

params(検索条件を渡す)



55
56
57
58
59
60
# File 'lib/railstar/search_base.rb', line 55

def initialize conditions={}
  conditions.each_pair do | key, val |
    writer_method = "#{key}="
    self.send(writer_method, val) if self.respond_to?(writer_method)
  end
end