5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/generators/pixelforce_cms/templates/arrayable.rb', line 5
def attr_arrayable(*args)
args.each do |arg|
target_class = arg.to_s.gsub('_ids', '').camelize
scope "by_#{arg}".gsub('_ids', '').to_sym, -> (object) do
case object
when Integer, String
where("? = ANY(#{self.table_name}.#{arg})", object)
when target_class.constantize
where("? = ANY(#{self.table_name}.#{arg})", object.id)
when Array
where("ARRAY[?] && #{arg}", object)
end
end
class_eval %Q(
def #{arg.to_s.gsub('_ids', '').pluralize}
#{target_class}.where(id: #{arg})
end
def #{arg}=(ids)
ids -= [0, nil, ""]
super(ids)
end
)
end
end
|