accepts-flattened-values 0.1.3 → 1.0.0
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/lib/accepts-flattened-values.rb +74 -0
- data/lib/accepts-flattened-values/version.rb +3 -0
- metadata +47 -73
- data/CHANGELOG +0 -11
- data/LICENSE +0 -22
- data/README +0 -27
- data/lib/accepts_flattened_values.rb +0 -11
- data/lib/accepts_flattened_values/mixin.rb +0 -106
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
require "active_support/concern"
|
|
2
|
+
require "active_support/core_ext/class/attribute"
|
|
3
|
+
|
|
4
|
+
module AcceptsFlattenedValues
|
|
5
|
+
require "accepts-flattened-values/version"
|
|
6
|
+
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
class_attribute :flattened_values_options, :instance_writer => false
|
|
11
|
+
self.flattened_values_options = {}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
module ClassMethods
|
|
15
|
+
def accepts_flattened_values_for(*attr_names)
|
|
16
|
+
options = { :separator => ",", :attribute => :value }
|
|
17
|
+
options.update(attr_names.extract_options!)
|
|
18
|
+
options.assert_valid_keys(:separator, :attribute)
|
|
19
|
+
|
|
20
|
+
attr_names.each do |association_name|
|
|
21
|
+
if reflection = reflect_on_association(association_name)
|
|
22
|
+
reflection.options[:autosave] = true
|
|
23
|
+
add_autosave_association_callbacks(reflection)
|
|
24
|
+
|
|
25
|
+
options = options.merge(:klass => reflection.klass)
|
|
26
|
+
self.flattened_values_options = flattened_values_options.merge(association_name.to_sym => options)
|
|
27
|
+
|
|
28
|
+
unless reflection.collection?
|
|
29
|
+
raise ArugmentError, "Assocation `#{association_name}' must be a has_many or has_and_belongs_to_many."
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# def pirate_values
|
|
33
|
+
# retrieve_flattened_values_for_association(:pirate)
|
|
34
|
+
# end
|
|
35
|
+
# def pirate_values=(string)
|
|
36
|
+
# assign_flattened_values_for_association(:pirate, string)
|
|
37
|
+
# end
|
|
38
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
39
|
+
if method_defined?(:#{association_name}_values)
|
|
40
|
+
remove_method(:#{association_name}_values)
|
|
41
|
+
end
|
|
42
|
+
def #{association_name}_values
|
|
43
|
+
retrieve_flattened_values_for_association(:#{association_name})
|
|
44
|
+
end
|
|
45
|
+
if method_defined?(:#{association_name}_values=)
|
|
46
|
+
remove_method(:#{association_name}_values=)
|
|
47
|
+
end
|
|
48
|
+
def #{association_name}_values=(string)
|
|
49
|
+
assign_flattened_values_for_association(:#{association_name}, string)
|
|
50
|
+
end
|
|
51
|
+
RUBY
|
|
52
|
+
else
|
|
53
|
+
raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
protected
|
|
60
|
+
def retrieve_flattened_values_for_association(association)
|
|
61
|
+
options = flattened_values_options[association]
|
|
62
|
+
send(association).map(&options[:attribute]).join(options[:separator])
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def assign_flattened_values_for_association(association, values)
|
|
66
|
+
options = flattened_values_options[association]
|
|
67
|
+
values = values.split(options[:separator])
|
|
68
|
+
|
|
69
|
+
records = values.map do |value|
|
|
70
|
+
options[:klass].where(options[:attribute] => value).first_or_initialize
|
|
71
|
+
end
|
|
72
|
+
send("#{association}=", records)
|
|
73
|
+
end
|
|
74
|
+
end
|
metadata
CHANGED
|
@@ -1,92 +1,66 @@
|
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: accepts-flattened-values
|
|
3
|
-
version: !ruby/object:Gem::Version
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
- 0
|
|
7
|
-
- 1
|
|
8
|
-
- 3
|
|
9
|
-
version: 0.1.3
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
prerelease:
|
|
10
6
|
platform: ruby
|
|
11
|
-
authors:
|
|
7
|
+
authors:
|
|
12
8
|
- Samuel Kadolph
|
|
13
9
|
autorequire:
|
|
14
10
|
bindir: bin
|
|
15
11
|
cert_chain: []
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
dependencies:
|
|
20
|
-
- !ruby/object:Gem::Dependency
|
|
12
|
+
date: 2012-06-13 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
21
15
|
name: activesupport
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
requirements:
|
|
25
|
-
- -
|
|
26
|
-
- !ruby/object:Gem::Version
|
|
27
|
-
|
|
28
|
-
- 0
|
|
29
|
-
version: "0"
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '3.2'
|
|
30
22
|
type: :runtime
|
|
31
|
-
version_requirements: *id001
|
|
32
|
-
- !ruby/object:Gem::Dependency
|
|
33
|
-
name: activerecord
|
|
34
23
|
prerelease: false
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ~>
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '3.2'
|
|
30
|
+
description: accepts-flattened-values is a mixin for ActiveRecord to be used on any
|
|
31
|
+
model with a has_many or has_and_belongs_to_many association. The purpose of this
|
|
32
|
+
mixin is to simplify a tag like association for any model or association.
|
|
33
|
+
email:
|
|
34
|
+
- samuel@kadolph.com
|
|
46
35
|
executables: []
|
|
47
|
-
|
|
48
36
|
extensions: []
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
-
|
|
52
|
-
-
|
|
53
|
-
-
|
|
54
|
-
files:
|
|
55
|
-
- CHANGELOG
|
|
56
|
-
- LICENSE
|
|
57
|
-
- README
|
|
58
|
-
- lib/accepts_flattened_values/mixin.rb
|
|
59
|
-
- lib/accepts_flattened_values.rb
|
|
60
|
-
has_rdoc: true
|
|
61
|
-
homepage: http://github.com/samuelkadolph/accepts-flattened-values/wiki
|
|
37
|
+
extra_rdoc_files: []
|
|
38
|
+
files:
|
|
39
|
+
- lib/accepts-flattened-values/version.rb
|
|
40
|
+
- lib/accepts-flattened-values.rb
|
|
41
|
+
homepage: http://samuelkadolph.github.com/accepts-flattened-values/
|
|
62
42
|
licenses: []
|
|
63
|
-
|
|
64
43
|
post_install_message:
|
|
65
|
-
rdoc_options:
|
|
66
|
-
|
|
67
|
-
- README
|
|
68
|
-
require_paths:
|
|
44
|
+
rdoc_options: []
|
|
45
|
+
require_paths:
|
|
69
46
|
- lib
|
|
70
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
requirements:
|
|
79
|
-
- -
|
|
80
|
-
- !ruby/object:Gem::Version
|
|
81
|
-
|
|
82
|
-
- 0
|
|
83
|
-
version: "0"
|
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
|
+
none: false
|
|
49
|
+
requirements:
|
|
50
|
+
- - ! '>='
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
none: false
|
|
55
|
+
requirements:
|
|
56
|
+
- - ! '>='
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '0'
|
|
84
59
|
requirements: []
|
|
85
|
-
|
|
86
60
|
rubyforge_project:
|
|
87
|
-
rubygems_version: 1.
|
|
61
|
+
rubygems_version: 1.8.21
|
|
88
62
|
signing_key:
|
|
89
63
|
specification_version: 3
|
|
90
|
-
summary: accepts-flattened-values is an ActiveRecord mixin to flatten a
|
|
64
|
+
summary: accepts-flattened-values is an ActiveRecord mixin to flatten a has_many or
|
|
65
|
+
has_and_belongs_to_many assocation.
|
|
91
66
|
test_files: []
|
|
92
|
-
|
data/CHANGELOG
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
==== accepts-flattened-values 0.1.3
|
|
2
|
-
* updated gem homepage to new github wiki
|
|
3
|
-
|
|
4
|
-
==== accepts-flattened-values 0.1.2
|
|
5
|
-
* updated gemspec to use activerecord and activesupport 3.0.0.rc
|
|
6
|
-
|
|
7
|
-
==== accepts-flattened-values 0.1.1
|
|
8
|
-
* renamed accepts-flattened-values.rb so it won't be auto required
|
|
9
|
-
|
|
10
|
-
==== accepts-flattened-values 0.1.0
|
|
11
|
-
* Initial version
|
data/LICENSE
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
This is free and unencumbered software released into the public domain.
|
|
2
|
-
|
|
3
|
-
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
-
distribute this software, either in source code form or as a compiled
|
|
5
|
-
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
-
means.
|
|
7
|
-
|
|
8
|
-
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
-
of this software dedicate any and all copyright interest in the
|
|
10
|
-
software to the public domain. We make this dedication for the benefit
|
|
11
|
-
of the public at large and to the detriment of our heirs and
|
|
12
|
-
successors. We intend this dedication to be an overt act of
|
|
13
|
-
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
-
software under copyright law.
|
|
15
|
-
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
-
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
-
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
-
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
data/README
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
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">]
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
module AcceptsFlattenedValues::Mixin
|
|
2
|
-
extend ActiveSupport::Concern
|
|
3
|
-
|
|
4
|
-
included do
|
|
5
|
-
class_inheritable_accessor :flattened_values_options, :flattened_values_klasses
|
|
6
|
-
self.flattened_values_options = {}
|
|
7
|
-
self.flattened_values_klasses = {}
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
module ClassMethods
|
|
11
|
-
# == Flattened Values
|
|
12
|
-
#
|
|
13
|
-
# Flattened values allow you to save single value entities in a has_many or has_and_belongs_to_many association.
|
|
14
|
-
# By default flattened values updating is turned off, you can enable it using the accepts_flattened_values_for
|
|
15
|
-
# class method. When you enable flattened values an attribute writer is defined on the model.
|
|
16
|
-
#
|
|
17
|
-
# The attribute writer is named after the association, which means that
|
|
18
|
-
# in the following example, two new methods are added to your model:
|
|
19
|
-
# <tt>interests_values=(values)</tt> and
|
|
20
|
-
# <tt>skills_values=(values)</tt>.
|
|
21
|
-
#
|
|
22
|
-
# class User < ActiveRecord::Base
|
|
23
|
-
# has_many :interests
|
|
24
|
-
# has_many :skills
|
|
25
|
-
#
|
|
26
|
-
# accepts_nested_attributes_for :interests, :skills
|
|
27
|
-
# end
|
|
28
|
-
#
|
|
29
|
-
# Note that the <tt>:autosave</tt> option is automatically enabled on every
|
|
30
|
-
# association that accepts_flattened_values_for is used for.
|
|
31
|
-
#
|
|
32
|
-
# === Options
|
|
33
|
-
# * <tt>:separator</tt> the separator used to join and split the string value. Defaults to ','
|
|
34
|
-
# * <tt>:value</tt> the field to use for the string values on the associatied model. Defaults to ':value'
|
|
35
|
-
def accepts_flattened_values_for(*attr_names)
|
|
36
|
-
options = { :separator => ',', :value => :value }
|
|
37
|
-
options.update(attr_names.extract_options!)
|
|
38
|
-
options.assert_valid_keys(:separator, :value)
|
|
39
|
-
|
|
40
|
-
attr_names.each do |association_name|
|
|
41
|
-
if reflection = reflect_on_association(association_name)
|
|
42
|
-
reflection.options[:autosave] = true
|
|
43
|
-
add_autosave_association_callbacks(reflection)
|
|
44
|
-
flattened_values_options[association_name.to_sym] = options
|
|
45
|
-
flattened_values_klasses[association_name.to_sym] = reflection.klass
|
|
46
|
-
|
|
47
|
-
unless reflection.collection?
|
|
48
|
-
raise ArugmentError, "Assocation `#{association_name}' must be a collection."
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
# def pirate_values
|
|
52
|
-
# get_flattened_values_for_association(:pirate)
|
|
53
|
-
# end
|
|
54
|
-
#
|
|
55
|
-
# def pirate_values=(values)
|
|
56
|
-
# assign_flattened_values_for_association(:pirate, values)
|
|
57
|
-
# end
|
|
58
|
-
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
|
59
|
-
if method_defined?(:#{association_name}_values)
|
|
60
|
-
remove_method(:#{association_name}_values)
|
|
61
|
-
end
|
|
62
|
-
def #{association_name}_values
|
|
63
|
-
get_flattened_values_for_association(:#{association_name})
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
if method_defined?(:#{association_name}_values=)
|
|
67
|
-
remove_method(:#{association_name}_values=)
|
|
68
|
-
end
|
|
69
|
-
def #{association_name}_values=(values)
|
|
70
|
-
assign_flattened_values_for_association(:#{association_name}, values)
|
|
71
|
-
end
|
|
72
|
-
RUBY_EVAL
|
|
73
|
-
else
|
|
74
|
-
raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
private
|
|
81
|
-
# Collects the <tt>options[:value]</tt>field from all the entities from an association and joins them together
|
|
82
|
-
# into a single string separated by <tt>options[:separator]</tt>.
|
|
83
|
-
def get_flattened_values_for_association(association_name)
|
|
84
|
-
options = flattened_values_options[association_name]
|
|
85
|
-
association = send(association_name)
|
|
86
|
-
|
|
87
|
-
association.collect(&options[:value]).join(options[:separator])
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
# Splits the given string with <tt>options[:separator]</tt> and for each value, calls
|
|
91
|
-
# <tt>find_or_create_by_#{option[:value]}</tt> then adds the existing or new entity's id to an array and assigns
|
|
92
|
-
# the array to <tt>singular_ids=</tt>.
|
|
93
|
-
def assign_flattened_values_for_association(association_name, values)
|
|
94
|
-
options = flattened_values_options[association_name]
|
|
95
|
-
klass = flattened_values_klasses[association_name]
|
|
96
|
-
values = values.split(options[:separator])
|
|
97
|
-
|
|
98
|
-
new_ids = []
|
|
99
|
-
values.each do |value|
|
|
100
|
-
object = klass.send(:"find_or_create_by_#{options[:value]}", value)
|
|
101
|
-
new_ids << object.id
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
send(:"#{association_name.to_s.singularize}_ids=", new_ids)
|
|
105
|
-
end
|
|
106
|
-
end
|