throughcheckboxes 0.0.4 → 0.0.5
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.
- data/.gitignore +2 -0
- data/README.textile +40 -2
- data/lib/throughcheckboxes/checkboxes_for.rb +6 -0
- data/lib/throughcheckboxes/core.rb +22 -5
- data/lib/throughcheckboxes/version.rb +2 -2
- data/throughcheckboxes.gemspec +1 -1
- metadata +2 -2
data/README.textile
CHANGED
@@ -1,3 +1,41 @@
|
|
1
|
-
|
1
|
+
h2. Introduction
|
2
2
|
|
3
|
-
|
3
|
+
This gem will help you to handle _has_many :through_ associations when you
|
4
|
+
need to add/remove these ones with checkboxes. If you think
|
5
|
+
about this type of situation you do the same steps for each association you
|
6
|
+
have to handle with checkboxes. I exctracted this gem from a project
|
7
|
+
where I had to handle about ten associations with checkboxes.
|
8
|
+
|
9
|
+
h2. How to use it
|
10
|
+
|
11
|
+
The best way to describe throughcheckboxes is to show some code:
|
12
|
+
|
13
|
+
In your model:
|
14
|
+
|
15
|
+
<pre>
|
16
|
+
class User < ActiveRecord::Base
|
17
|
+
|
18
|
+
# adds your associations
|
19
|
+
checkboxes_for :groups
|
20
|
+
end
|
21
|
+
</pre>
|
22
|
+
|
23
|
+
In your view:
|
24
|
+
|
25
|
+
<pre>
|
26
|
+
<%= form_for @user do |f| %>
|
27
|
+
<%= f.error_messages %>
|
28
|
+
<%= f.checkboxes_for :groups %>
|
29
|
+
</p>
|
30
|
+
<p><%= f.submit %></p>
|
31
|
+
<% end %>
|
32
|
+
</pre>
|
33
|
+
|
34
|
+
and that's all. Now, when you submit your form you get the
|
35
|
+
has_many :through association updated.
|
36
|
+
|
37
|
+
h2. Roadmap
|
38
|
+
|
39
|
+
* write tests
|
40
|
+
* write options for helper
|
41
|
+
* write more helpers
|
@@ -1,11 +1,17 @@
|
|
1
1
|
module ThroughCheckboxes
|
2
2
|
|
3
|
+
# This module contains all the methods that _concern_ with
|
4
|
+
# ActiveRecord::Base.
|
3
5
|
module Checkboxesfor
|
4
6
|
|
5
7
|
extend ActiveSupport::Concern
|
6
8
|
|
7
9
|
module ClassMethods
|
8
10
|
|
11
|
+
# Use it in your model for the relations you need to
|
12
|
+
# handle with checkboxes.
|
13
|
+
# @return [nil]
|
14
|
+
# @raise ThroughCheckboxesNameError If any relation is not valid.
|
9
15
|
def checkboxes_for(*relations)
|
10
16
|
|
11
17
|
klass = self
|
@@ -1,12 +1,21 @@
|
|
1
1
|
module ThroughCheckboxes
|
2
2
|
|
3
|
-
module Core
|
3
|
+
module Core # This module contains utility methods for both extensions and helpers.
|
4
4
|
|
5
5
|
class ThroughCheckboxesNameError < StandardError; end
|
6
6
|
|
7
|
-
|
7
|
+
# Checks the validity of the relations
|
8
|
+
# on a given class.
|
9
|
+
# A relation will be valid:
|
10
|
+
# * It exists
|
11
|
+
# * It's an _has_many
|
12
|
+
# * It uses the _through_ option
|
13
|
+
# @todo makes error raised more specific
|
14
|
+
# @raise ThroughCheckboxesNameError Generic Error
|
15
|
+
# @return [boolean] It returns _true_, it raises an exception if the validation fails.
|
16
|
+
def validate(klass, *relations)
|
8
17
|
for relation in relations
|
9
|
-
unless (klass.reflections[relation].present? &&
|
18
|
+
unless (klass.reflections[relation].present? &&
|
10
19
|
klass.reflections[relation].macro == :has_many &&
|
11
20
|
klass.reflections[relation].options[:through].present?)
|
12
21
|
raise ThroughCheckboxesNameError, "#{relation} isn't an has_many :through for model #{klass}, check it out please."
|
@@ -14,16 +23,24 @@ module ThroughCheckboxes
|
|
14
23
|
end
|
15
24
|
end
|
16
25
|
|
17
|
-
|
26
|
+
# Returns the class of a relation of
|
27
|
+
# of a given class.
|
28
|
+
# It cares about the _source_ option of the
|
29
|
+
# _has_many_.
|
30
|
+
# @return [class]
|
31
|
+
def relation_klass(klass, relation)
|
18
32
|
source=klass.reflections[relation].options[:source]
|
19
33
|
relation_klass = source.present? ? source.to_s : relation.to_s
|
20
34
|
relation_klass.classify.constantize
|
21
35
|
end
|
22
36
|
|
23
|
-
|
37
|
+
# Returns a pattern name for the attr_accessor used for handling checkboxes.
|
38
|
+
# @return [String]
|
39
|
+
def field_name(relation)
|
24
40
|
"#{relation}_ids"
|
25
41
|
end
|
26
42
|
|
43
|
+
extend self
|
27
44
|
|
28
45
|
end
|
29
46
|
|
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = "0.0.
|
1
|
+
module ThroughCheckboxes
|
2
|
+
VERSION = "0.0.5"
|
3
3
|
end
|
data/throughcheckboxes.gemspec
CHANGED
@@ -4,7 +4,7 @@ require "throughcheckboxes/version"
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "throughcheckboxes"
|
7
|
-
s.version =
|
7
|
+
s.version = ThroughCheckboxes::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["lucapette"]
|
10
10
|
s.email = ["lucapette@gmail.com"]
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: throughcheckboxes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- lucapette
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-03-
|
13
|
+
date: 2011-03-19 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|