accepts-flattened-values 0.1.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.
@@ -0,0 +1,2 @@
1
+ ==== accepts-flattened-values 1.0.0
2
+ * Initial version
data/LICENSE ADDED
@@ -0,0 +1,22 @@
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 ADDED
File without changes
@@ -0,0 +1,9 @@
1
+ require 'active_support'
2
+
3
+ module AcceptsFlattenedValues
4
+ autoload :Mixin, 'accepts_flattened_values/mixin'
5
+ end
6
+
7
+ ActiveSupport.on_load(:active_record) do
8
+ ActiveRecord::Base.send(:include, AcceptsFlattenedValues::Mixin)
9
+ end
@@ -0,0 +1,79 @@
1
+ require 'active_support/concern'
2
+
3
+ module AcceptsFlattenedValues::Mixin
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_inheritable_accessor :flattened_values_options, :flattened_values_klasses
8
+ self.flattened_values_options = {}
9
+ self.flattened_values_klasses = {}
10
+ end
11
+
12
+ module ClassMethods
13
+ def accepts_flattened_values_for(*attr_names)
14
+ options = { :separator => ',', :value => :value }
15
+ options.update(attr_names.extract_options!)
16
+ options.assert_valid_keys(:separator, :value)
17
+
18
+ attr_names.each do |association_name|
19
+ if reflection = reflect_on_association(association_name)
20
+ reflection.options[:autosave] = true
21
+ add_autosave_association_callbacks(reflection)
22
+ flattened_values_options[association_name.to_sym] = options
23
+ flattened_values_klasses[association_name.to_sym] = reflection.klass
24
+
25
+ unless reflection.collection?
26
+ raise ArugmentError, "Assocation `#{association_name}' must be a collection."
27
+ end
28
+
29
+ # def pirate_values
30
+ # get_flattened_values_for_association(:pirate)
31
+ # end
32
+ #
33
+ # def pirate_values=(values)
34
+ # assign_flattened_values_for_association(:pirate, values)
35
+ # end
36
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
37
+ if method_defined?(:#{association_name}_values)
38
+ remove_method(:#{association_name}_values)
39
+ end
40
+ def #{association_name}_values
41
+ get_flattened_values_for_association(:#{association_name})
42
+ end
43
+
44
+ if method_defined?(:#{association_name}_values=)
45
+ remove_method(:#{association_name}_values=)
46
+ end
47
+ def #{association_name}_values=(values)
48
+ assign_flattened_values_for_association(:#{association_name}, values)
49
+ end
50
+ RUBY_EVAL
51
+ else
52
+ raise ArgumentError, "No association found for name `#{association_name}'. Has it been defined yet?"
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ private
59
+ def get_flattened_values_for_association(association_name)
60
+ options = flattened_values_options[association_name]
61
+ association = send(association_name)
62
+
63
+ association.collect(&options[:value]).join(options[:separator])
64
+ end
65
+
66
+ def assign_flattened_values_for_association(association_name, values)
67
+ options = flattened_values_options[association_name]
68
+ klass = flattened_values_klasses[association_name]
69
+ values = values.split(options[:separator])
70
+
71
+ new_ids = []
72
+ values.each do |value|
73
+ object = klass.send(:"find_or_create_by_#{options[:value]}", value)
74
+ new_ids << object.id
75
+ end
76
+
77
+ send(:"#{association_name.to_s.singularize}_ids=", new_ids)
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: accepts-flattened-values
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Samuel Kadolph
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-19 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ - beta4
32
+ version: 3.0.0.beta4
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 3
44
+ - 0
45
+ - 0
46
+ - beta4
47
+ version: 3.0.0.beta4
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: accepts-flattened-values is mixin for activerecord that flattens single values from a has and belongs to many assocations into a string and vice versa.
51
+ email: samuel@kadolph.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - CHANGELOG
60
+ - README
61
+ - LICENSE
62
+ - lib/accepts-flattened-values.rb
63
+ - lib/accepts_flattened_values/mixin.rb
64
+ has_rdoc: true
65
+ homepage: http://github.com/samuelkadolph/accepts-flattened-values
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.6
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: accepts-flattened-values is an activerecord mixin to flatten a habtm assocation.
94
+ test_files: []
95
+