throughcheckboxes 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,19 +5,35 @@ module ThroughCheckboxes
|
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
-
|
9
|
-
|
8
|
+
|
9
|
+
def checkboxes_for(*relations)
|
10
|
+
|
11
|
+
klass = self
|
12
|
+
|
13
|
+
ThroughCheckboxes::Core.validate(klass, *relations)
|
14
|
+
|
15
|
+
for relation in relations
|
16
|
+
|
17
|
+
field_name="#{relation}_ids"
|
18
|
+
|
19
|
+
class_eval do
|
20
|
+
attr_accessible field_name.to_sym
|
21
|
+
end
|
22
|
+
|
10
23
|
instance_eval do
|
11
|
-
define_method "#{
|
12
|
-
|
24
|
+
define_method "#{field_name}=" do |ids|
|
25
|
+
send("#{relation}=",ids.select { |id| id.present? }.map {|id| ThroughCheckboxes::Core.relation_klass(klass, relation).find(id)})
|
13
26
|
end
|
14
27
|
|
15
|
-
define_method
|
16
|
-
|
28
|
+
define_method field_name do
|
29
|
+
send("#{relation}").map {|e| e.id}
|
17
30
|
end
|
18
31
|
end
|
32
|
+
|
19
33
|
end
|
34
|
+
|
20
35
|
end
|
36
|
+
|
21
37
|
end
|
22
38
|
|
23
39
|
module InstanceMethods
|
@@ -7,7 +7,7 @@ module ThroughCheckboxes
|
|
7
7
|
def checkboxes_for(relation)
|
8
8
|
output = ""
|
9
9
|
output << "#{self.label(relation)}<br />"
|
10
|
-
for record in
|
10
|
+
for record in ThroughCheckboxes::Core.relation_klass(self.object.class, relation).all
|
11
11
|
output << @template.check_box_tag("#{@object_name}[#{relation}_ids][]",record.id,self.object.send(relation).include?(record))
|
12
12
|
output << "#{record}<br />"
|
13
13
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ThroughCheckboxes
|
2
|
+
|
3
|
+
module Core
|
4
|
+
|
5
|
+
class ThroughCheckboxesNameError < StandardError; end
|
6
|
+
|
7
|
+
def self.validate(klass, *relations)
|
8
|
+
for relation in relations
|
9
|
+
unless (klass.reflections[relation].present? &&
|
10
|
+
klass.reflections[relation].macro == :has_many &&
|
11
|
+
klass.reflections[relation].options[:through].present?)
|
12
|
+
raise ThroughCheckboxesNameError, "#{relation} isn't an has_many :through for model #{klass}, check it out please."
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.relation_klass(klass, relation)
|
18
|
+
source=klass.reflections[relation].options[:source]
|
19
|
+
relation_klass = source.present? ? source.to_s : relation.to_s
|
20
|
+
relation_klass.classify.constantize
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/lib/throughcheckboxes.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- lucapette
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- lib/throughcheckboxes.rb
|
36
36
|
- lib/throughcheckboxes/checkboxes_for.rb
|
37
37
|
- lib/throughcheckboxes/checkboxes_for_helpers.rb
|
38
|
+
- lib/throughcheckboxes/core.rb
|
38
39
|
- lib/throughcheckboxes/version.rb
|
39
40
|
- throughcheckboxes.gemspec
|
40
41
|
has_rdoc: true
|