Module: Checkboxes::Methods::ClassMethods

Defined in:
lib/checkboxes/methods.rb

Instance Method Summary collapse

Instance Method Details

#checkboxes_for(*relations) ⇒ nil

Use it in your model for the relations you need to handle with checkboxes.

Returns:

  • (nil)

Raises:

  • CheckboxesNameError If any relation is not valid.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/checkboxes/methods.rb', line 15

def checkboxes_for(*relations)

  klass = self

  Checkboxes::Core.validate(klass, *relations)

  relations.each do |relation|

    field_name=Checkboxes::Core.field_name(relation)

    attr_accessible field_name.to_sym

    define_method "#{field_name}=" do |ids|
      send("#{relation}=",ids.select { |id| id.present? }.map {|id| Checkboxes::Core.relation_klass(klass, relation).find(id)})
    end

    define_method field_name do
      send("#{relation}").map {|e| e.id}
    end

  end

end