accepts-flattened-values 0.1.0 → 0.1.1
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/CHANGELOG +4 -1
- data/README +27 -0
- data/lib/{accepts-flattened-values.rb → accepts_flattened_values.rb} +0 -0
- data/lib/accepts_flattened_values/mixin.rb +29 -0
- metadata +16 -13
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
== accepts-flattened-values
|
2
|
+
|
3
|
+
accepts-flattenved-values is a mixin for ActiveRecord to be used on any model with a has_many or
|
4
|
+
has_and_belongs_to_many association. The primary purpose of this mixin is to simplify a tag like association for any
|
5
|
+
model or association.
|
6
|
+
|
7
|
+
== Getting Started
|
8
|
+
|
9
|
+
|
10
|
+
== Example
|
11
|
+
|
12
|
+
create_table :users do { |t| t.string :name, :null => false }
|
13
|
+
create_table :interests do { |t| t.string :value, :null => false }
|
14
|
+
create_table :interests_users, :id => false { |t| t.references :interest; t.references :user }
|
15
|
+
|
16
|
+
class User < ActiveRecord::Base
|
17
|
+
accepts_flattened_values_for :interests
|
18
|
+
end
|
19
|
+
|
20
|
+
u = User.create(:name => "Sam")
|
21
|
+
u.interests.create(:value => "ruby")
|
22
|
+
u.interests.create(:value => "ruby on rails")
|
23
|
+
u.interests.create(:value => "soccer")
|
24
|
+
|
25
|
+
u.interests_values # => "ruby,ruby on rails,soccer"
|
26
|
+
u.interests_values = "ruby,soccer,whatever"
|
27
|
+
u.interests # => [#<Interest value: "ruby">, <Interest value: "soccer">, <Interest value: "whatever">]
|
File without changes
|
@@ -10,6 +10,30 @@ module AcceptsFlattenedValues::Mixin
|
|
10
10
|
end
|
11
11
|
|
12
12
|
module ClassMethods
|
13
|
+
# == Flattened Values
|
14
|
+
#
|
15
|
+
# Flattened values allow you to save single value entities in a has_many or has_and_belongs_to_many association.
|
16
|
+
# By default flattened values updating is turned off, you can enable it using the accepts_flattened_values_for
|
17
|
+
# class method. When you enable flattened values an attribute writer is defined on the model.
|
18
|
+
#
|
19
|
+
# The attribute writer is named after the association, which means that
|
20
|
+
# in the following example, two new methods are added to your model:
|
21
|
+
# <tt>interests_values=(values)</tt> and
|
22
|
+
# <tt>skills_values=(values)</tt>.
|
23
|
+
#
|
24
|
+
# class User < ActiveRecord::Base
|
25
|
+
# has_many :interests
|
26
|
+
# has_many :skills
|
27
|
+
#
|
28
|
+
# accepts_nested_attributes_for :interests, :skills
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# Note that the <tt>:autosave</tt> option is automatically enabled on every
|
32
|
+
# association that accepts_flattened_values_for is used for.
|
33
|
+
#
|
34
|
+
# === Options
|
35
|
+
# * <tt>:separator</tt> the separator used to join and split the string value. Defaults to ','
|
36
|
+
# * <tt>:value</tt> the field to use for the string values on the associatied model. Defaults to ':value'
|
13
37
|
def accepts_flattened_values_for(*attr_names)
|
14
38
|
options = { :separator => ',', :value => :value }
|
15
39
|
options.update(attr_names.extract_options!)
|
@@ -56,6 +80,8 @@ module AcceptsFlattenedValues::Mixin
|
|
56
80
|
end
|
57
81
|
|
58
82
|
private
|
83
|
+
# Collects the <tt>options[:value]</tt>field from all the entities from an association and joins them together
|
84
|
+
# into a single string separated by <tt>options[:separator]</tt>.
|
59
85
|
def get_flattened_values_for_association(association_name)
|
60
86
|
options = flattened_values_options[association_name]
|
61
87
|
association = send(association_name)
|
@@ -63,6 +89,9 @@ module AcceptsFlattenedValues::Mixin
|
|
63
89
|
association.collect(&options[:value]).join(options[:separator])
|
64
90
|
end
|
65
91
|
|
92
|
+
# Splits the given string with <tt>options[:separator]</tt> and for each value, calls
|
93
|
+
# <tt>find_or_create_by_#{option[:value]}</tt> then adds the existing or new entity's id to an array and assigns
|
94
|
+
# the array to <tt>singular_ids=</tt>.
|
66
95
|
def assign_flattened_values_for_association(association_name, values)
|
67
96
|
options = flattened_values_options[association_name]
|
68
97
|
klass = flattened_values_klasses[association_name]
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Samuel Kadolph
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-07-
|
17
|
+
date: 2010-07-20 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - "
|
25
|
+
- - "="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
28
|
- 3
|
@@ -37,7 +37,7 @@ dependencies:
|
|
37
37
|
prerelease: false
|
38
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
39
|
requirements:
|
40
|
-
- - "
|
40
|
+
- - "="
|
41
41
|
- !ruby/object:Gem::Version
|
42
42
|
segments:
|
43
43
|
- 3
|
@@ -47,27 +47,30 @@ dependencies:
|
|
47
47
|
version: 3.0.0.beta4
|
48
48
|
type: :runtime
|
49
49
|
version_requirements: *id002
|
50
|
-
description: accepts-flattened-values is mixin for
|
50
|
+
description: accepts-flattened-values is mixin for ActiveRecord that flattens single values from a has_many or has_and_belongs_to_many assocation into a string and vice versa.
|
51
51
|
email: samuel@kadolph.com
|
52
52
|
executables: []
|
53
53
|
|
54
54
|
extensions: []
|
55
55
|
|
56
|
-
extra_rdoc_files:
|
57
|
-
|
56
|
+
extra_rdoc_files:
|
57
|
+
- CHANGELOG
|
58
|
+
- README
|
59
|
+
- LICENSE
|
58
60
|
files:
|
59
61
|
- CHANGELOG
|
60
62
|
- README
|
61
63
|
- LICENSE
|
62
|
-
- lib/accepts-flattened-values.rb
|
63
64
|
- lib/accepts_flattened_values/mixin.rb
|
65
|
+
- lib/accepts_flattened_values.rb
|
64
66
|
has_rdoc: true
|
65
|
-
homepage: http://github.com/samuelkadolph/accepts-flattened-values
|
67
|
+
homepage: http://wiki.github.com/samuelkadolph/accepts-flattened-values/
|
66
68
|
licenses: []
|
67
69
|
|
68
70
|
post_install_message:
|
69
|
-
rdoc_options:
|
70
|
-
|
71
|
+
rdoc_options:
|
72
|
+
- --main
|
73
|
+
- README
|
71
74
|
require_paths:
|
72
75
|
- lib
|
73
76
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -90,6 +93,6 @@ rubyforge_project:
|
|
90
93
|
rubygems_version: 1.3.6
|
91
94
|
signing_key:
|
92
95
|
specification_version: 3
|
93
|
-
summary: accepts-flattened-values is an
|
96
|
+
summary: accepts-flattened-values is an ActiveRecord mixin to flatten a habtm assocation.
|
94
97
|
test_files: []
|
95
98
|
|