mm-nested-attrs 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +0 -0
- data/lib/mm-nested-attrs.rb +60 -0
- metadata +48 -0
data/README.txt
ADDED
File without changes
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Plugins
|
3
|
+
module Associations
|
4
|
+
module NestedAttributes
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def accepts_nested_attributes_for(*attr_names)
|
9
|
+
options = { :allow_destroy => false }
|
10
|
+
options.update(attr_names.extract_options!)
|
11
|
+
options.assert_valid_keys(:allow_destroy, :reject_if)
|
12
|
+
|
13
|
+
for association_name in attr_names
|
14
|
+
module_eval %{
|
15
|
+
def #{association_name}_attributes=(attributes)
|
16
|
+
assign_nested_attributes_for_association(:#{association_name}, attributes, #{options[:allow_destroy]})
|
17
|
+
end
|
18
|
+
}, __FILE__, __LINE__
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module InstanceMethods
|
24
|
+
UNASSIGNABLE_KEYS = %w{ id _id _destroy }
|
25
|
+
|
26
|
+
def assign_nested_attributes_for_association(association_name, attributes_collection, allow_destroy)
|
27
|
+
class_name = self.associations[association_name].options[:class_name] || class_name
|
28
|
+
|
29
|
+
unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
|
30
|
+
raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
|
31
|
+
end
|
32
|
+
|
33
|
+
if attributes_collection.is_a? Hash
|
34
|
+
attributes_collection = attributes_collection.sort_by { |index, _| index.to_i }.map { |_, attributes| attributes }
|
35
|
+
end
|
36
|
+
|
37
|
+
attributes_collection.each do |attributes|
|
38
|
+
attributes.stringify_keys!
|
39
|
+
|
40
|
+
if attributes['_id'].blank?
|
41
|
+
send(association_name) << class_name.constantize.new(attributes)
|
42
|
+
elsif existing_record = send(association_name).detect { |record| record.id.to_s == attributes['_id'].to_s }
|
43
|
+
if existing_record.has_destroy_flag?(attributes) && allow_destroy
|
44
|
+
send(association_name).delete(existing_record)
|
45
|
+
existing_record.destroy unless class_name.constantize.embeddable?
|
46
|
+
else
|
47
|
+
existing_record.attributes = attributes.except(*UNASSIGNABLE_KEYS)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def has_destroy_flag?(hash)
|
54
|
+
Boolean.to_mongo(hash['_destroy'])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mm-nested-attrs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sergiy Shevchik
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: allows accepts_nested_attributes_for in models
|
15
|
+
email: berrouz@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.txt
|
20
|
+
files:
|
21
|
+
- lib/mm-nested-attrs.rb
|
22
|
+
- README.txt
|
23
|
+
homepage: https://github.com/berrouz/mm-nested-attrs
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.9.3
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.11
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: MongoMapper plugin for nested attributes
|
47
|
+
test_files: []
|
48
|
+
has_rdoc:
|