nested_attr 0.0.1.1 → 0.0.1.2

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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +74 -1
  3. data/lib/nested_attr.rb +43 -25
  4. data/lib/nested_attr/version.rb +1 -1
  5. metadata +31 -111
  6. data/test/dummy/README.rdoc +0 -28
  7. data/test/dummy/Rakefile +0 -6
  8. data/test/dummy/app/assets/javascripts/application.js +0 -13
  9. data/test/dummy/app/assets/stylesheets/application.css +0 -15
  10. data/test/dummy/app/controllers/application_controller.rb +0 -5
  11. data/test/dummy/app/helpers/application_helper.rb +0 -2
  12. data/test/dummy/app/models/category.rb +0 -3
  13. data/test/dummy/app/models/post.rb +0 -4
  14. data/test/dummy/app/models/user.rb +0 -3
  15. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  16. data/test/dummy/bin/bundle +0 -3
  17. data/test/dummy/bin/rails +0 -4
  18. data/test/dummy/bin/rake +0 -4
  19. data/test/dummy/bin/setup +0 -29
  20. data/test/dummy/config.ru +0 -4
  21. data/test/dummy/config/application.rb +0 -26
  22. data/test/dummy/config/boot.rb +0 -5
  23. data/test/dummy/config/database.yml +0 -25
  24. data/test/dummy/config/environment.rb +0 -5
  25. data/test/dummy/config/environments/development.rb +0 -41
  26. data/test/dummy/config/environments/production.rb +0 -79
  27. data/test/dummy/config/environments/test.rb +0 -42
  28. data/test/dummy/config/initializers/assets.rb +0 -11
  29. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  30. data/test/dummy/config/initializers/cookies_serializer.rb +0 -3
  31. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  32. data/test/dummy/config/initializers/inflections.rb +0 -16
  33. data/test/dummy/config/initializers/mime_types.rb +0 -4
  34. data/test/dummy/config/initializers/session_store.rb +0 -3
  35. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  36. data/test/dummy/config/locales/en.yml +0 -23
  37. data/test/dummy/config/routes.rb +0 -56
  38. data/test/dummy/config/secrets.yml +0 -22
  39. data/test/dummy/db/development.sqlite3 +0 -0
  40. data/test/dummy/db/migrate/20151004115649_create_users.rb +0 -9
  41. data/test/dummy/db/migrate/20151004120032_create_posts.rb +0 -11
  42. data/test/dummy/db/migrate/20151004124603_create_categories.rb +0 -9
  43. data/test/dummy/db/migrate/20151004131127_categories_posts.rb +0 -14
  44. data/test/dummy/db/schema.rb +0 -51
  45. data/test/dummy/db/test.sqlite3 +0 -0
  46. data/test/dummy/log/development.log +0 -52
  47. data/test/dummy/log/test.log +0 -888
  48. data/test/dummy/public/404.html +0 -67
  49. data/test/dummy/public/422.html +0 -67
  50. data/test/dummy/public/500.html +0 -66
  51. data/test/dummy/public/favicon.ico +0 -0
  52. data/test/dummy/test/fixtures/posts.yml +0 -11
  53. data/test/dummy/test/fixtures/users.yml +0 -7
  54. data/test/dummy/test/models/category_test.rb +0 -7
  55. data/test/dummy/test/models/post_test.rb +0 -7
  56. data/test/dummy/test/models/user_test.rb +0 -7
  57. data/test/factories.rb +0 -31
  58. data/test/nested_attr_test.rb +0 -26
  59. data/test/test_helper.rb +0 -20
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f781c8988e43033b9556a6f96e6a2b375b46e13f
4
- data.tar.gz: ad29ec838e4a7c3e131f7eebe6ffab6c8ab515e0
3
+ metadata.gz: 5d2a84956552203b409cdca64049afca02b273f7
4
+ data.tar.gz: ada7bf061a635d534e2783543a9447830f4f2a97
5
5
  SHA512:
6
- metadata.gz: 88ac39de6b22ce9fc001b5fe26ceca8121b88af6e00ea8c6eab6a9ec0559ce166b9d3a3b548e5cc8caa5d2825e601b112587df9e6d73605bc4913fd035dbbf8e
7
- data.tar.gz: 7136f8ee3b7b22de1e7d805973707e73b1d913db3d38717b3120c2ccd176862a8a0699c37e8fface05be043a7d38dc4da1b4c1a131e8fc268efe20afa7ee8152
6
+ metadata.gz: f0011d212a9b0d52951c70c5efa540f68197961322a9357afe0875846a22fcc61697db442f5f1cf6585317119d49319d0f905e7f11a37dc53de13ea8e3b40ea9
7
+ data.tar.gz: 7b904ae6831f286e0ec7224c21b7de95341da59188e2cc99ed329f9427ddd00b9a1db87e53e9b49844e1a182b4eb6a1cd8e4469f03f2accc76f5689e69789088
data/README.rdoc CHANGED
@@ -1,3 +1,76 @@
1
1
  = NestedAttr
2
+ =====Using this plugin you can easily test your Active Admin formastic forms.
2
3
 
3
- This project rocks and uses MIT-LICENSE.
4
+ == Usage
5
+ === Install:
6
+ gem 'nested_attr'
7
+ === Basic Usage
8
+ require 'nested_attr'
9
+ NestedAttr.nested_attr_for(:user)
10
+
11
+ === Usage with active admin
12
+ Suppose you have a blog and you have these factories:
13
+
14
+ FactoryGirl.define do
15
+ factory :admin_user, :class => AdminUser do
16
+ sequence(:email){|n| "email#{n}@example.com"}
17
+ password "password"
18
+ password_confirmation "password"
19
+ end
20
+
21
+ factory :post, class: Post do
22
+ user
23
+ title "this is a title"
24
+ body "this is a huge body"
25
+
26
+ after :build do |p|
27
+ p.categories << create(:category)
28
+ end
29
+ end
30
+
31
+ factory :category, class: Category do
32
+ sequence :name do |n|
33
+ "category#{n}"
34
+ end
35
+ end
36
+ end
37
+
38
+ You have active admin installed at your rails app. At the form of your active admin you have following:
39
+
40
+
41
+ ActiveAdmin.register Post do
42
+ permit_params :post
43
+
44
+ form do |f|
45
+ f.inputs :title, :body
46
+ f.inputs :categories
47
+
48
+ f.has_many :tags do |t|
49
+ t.input :name
50
+ end
51
+
52
+ f.actions
53
+ end
54
+
55
+ end
56
+
57
+ Now you want to test your controller using rspec. How? Very simple. Like this:
58
+
59
+ include Devise::TestHelpers
60
+
61
+ RSpec.describe Admin::PostsController, :type => :controller do
62
+ before(:each) do
63
+ @user = FactoryGirl.create(:admin_user)
64
+ sign_in @user
65
+ end
66
+
67
+ describe "Create" do
68
+ it "creates post" do
69
+ expect{post :create, post: NestedAttr.nested_attr_for(:post, has_many=[:tags])}.to change{Post.count}.by(1)
70
+ end
71
+ end
72
+ end
73
+
74
+
75
+
76
+ This project rocks and uses MIT-LICENSE.
data/lib/nested_attr.rb CHANGED
@@ -1,15 +1,6 @@
1
1
  module NestedAttr
2
2
  require "factory_girl"
3
- @@except=[]
4
- @@include_id=[]
5
3
 
6
- def self.except(m)
7
- @@except <<m
8
- end
9
-
10
- def self.include(i)
11
- @@include_id<<i
12
- end
13
4
 
14
5
  class NestedAttributesStrategy
15
6
  # Author: Sadaf Noor (email@sadafnoor.com)
@@ -53,47 +44,76 @@ module NestedAttr
53
44
 
54
45
  unless instance.instance_variables.include? :@attributes then return {} end
55
46
  if @created_model.include? instance.class then return {} end
47
+
56
48
  @created_model << instance.class
57
49
 
58
50
  attrs = instance.attributes.delete_if do |k, _|
59
51
  ( %w(id type created_at updated_at).include?(k) or k.end_with?("_id") )
60
52
  end
61
53
 
62
- nested_reflections_has_many(instance).each do |ref|
63
- attrs.merge!("#{ref.name}_attributes" => instance.send(ref.name).each_with_index.map do |nested_obj,i|
64
- {i.to_s => attributes(nested_obj)}
65
- end [0])
66
- end
54
+
67
55
  nested_reflections_belongs_to(instance).each do |ref|
68
56
  attrs.merge!("#{ref.name}" => attributes(instance.send(ref.name)))
69
57
  end
58
+
59
+ nested_reflections_has_many(instance).each do |ref|
60
+ if @@has_many_attributes.include? ref.name
61
+ attrs.merge!("#{ref.name}_attributes" => instance.send(ref.name).each_with_index.map do |nested_obj,i|
62
+ {i.to_s => attributes(nested_obj)}
63
+ end [0])
64
+ else
65
+ attrs.merge!("#{ref.name.to_s.singularize}_ids" => instance.send(ref.name).each.map do |nested_obj|
66
+ nested_obj.id.to_s
67
+ end)
68
+ end
69
+
70
+ end
70
71
 
71
72
  nested_reflections_has_and_belongs_to_many(instance).each do |ref|
72
- attrs.merge!("#{ref.name.to_s.singularize}_ids" => instance.send(ref.name).each.map do |nested_obj|
73
- nested_obj.id.to_s
74
- end)
73
+ #puts ref.name
74
+ #puts "+++"
75
+ #puts @@allowd_nested_attributes[0].class
76
+ #puts @@allowd_nested_attributes.include? ref.name
77
+ #puts "---"
78
+
79
+ if @@has_many_attributes.include? ref.name
80
+ attrs.merge!("#{ref.name}_attributes" => instance.send(ref.name).each_with_index.map do |nested_obj,i|
81
+ {i.to_s => attributes(nested_obj)}
82
+ end [0])
83
+ else
84
+ attrs.merge!("#{ref.name.to_s.singularize}_ids" => instance.send(ref.name).each.map do |nested_obj|
85
+ nested_obj.id.to_s
86
+ end)
87
+ end
75
88
  end
89
+
90
+
91
+
92
+
76
93
 
94
+
77
95
  instance.delete
78
96
 
79
97
  attrs
80
98
  end
81
99
 
100
+
101
+
82
102
  def nested_reflections_has_and_belongs_to_many(instance)
83
103
  instance.class.reflections.values.select do |ref|
84
- ref.macro == :has_and_belongs_to_many
104
+ ref.macro == :has_and_belongs_to_many
85
105
  end
86
106
  end
87
107
 
88
108
  def nested_reflections_has_many(instance)
89
109
  instance.class.reflections.values.select do |ref|
90
- ref.macro == :has_many && !@@except.include?(ref.name) && instance.respond_to?("#{ref.name}_attributes=")
110
+ ref.macro == :has_many && instance.respond_to?("#{ref.name}_attributes=")
91
111
  end
92
112
  end
93
113
 
94
114
  def nested_reflections_belongs_to(instance)
95
115
  instance.class.reflections.values.select do |ref|
96
- ref.macro == :belongs_to && !@@except.include?(ref.name) && instance.respond_to?("#{ref.name}")
116
+ ref.macro == :belongs_to && instance.respond_to?("#{ref.name}")
97
117
  end
98
118
  end
99
119
 
@@ -101,15 +121,13 @@ module NestedAttr
101
121
 
102
122
  if not FactoryGirl.methods.include? :nested_attr_for
103
123
 
104
- puts "registered as 'FactoryGirl.nested_attr_for(:factory_name)'"
124
+ #puts "registered as 'FactoryGirl.nested_attr_for(:factory_name)'"
105
125
  FactoryGirl.register_strategy(:nested_attr_for, NestedAttributesStrategy)
106
126
 
107
127
  end
108
128
 
109
- def self.nested_attr_for(factory, except=[])
110
- unless except.empty?
111
- @@except += except
112
- end
129
+ def self.nested_attr_for(factory,has_many=[])
130
+ @@has_many_attributes = has_many
113
131
 
114
132
  FactoryGirl.nested_attr_for(factory)
115
133
 
@@ -1,3 +1,3 @@
1
1
  module NestedAttr
2
- VERSION = "0.0.1.1"
2
+ VERSION = "0.0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_attr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.1
4
+ version: 0.0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadaf Noor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-04 00:00:00.000000000 Z
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description: Using this plugin you can use Factory Girl to test your formastic forms
42
70
  like that of active admin without much hassle.
43
71
  email:
@@ -52,60 +80,6 @@ files:
52
80
  - lib/nested_attr.rb
53
81
  - lib/nested_attr/version.rb
54
82
  - lib/tasks/nested_attr_tasks.rake
55
- - test/dummy/README.rdoc
56
- - test/dummy/Rakefile
57
- - test/dummy/app/assets/javascripts/application.js
58
- - test/dummy/app/assets/stylesheets/application.css
59
- - test/dummy/app/controllers/application_controller.rb
60
- - test/dummy/app/helpers/application_helper.rb
61
- - test/dummy/app/models/category.rb
62
- - test/dummy/app/models/post.rb
63
- - test/dummy/app/models/user.rb
64
- - test/dummy/app/views/layouts/application.html.erb
65
- - test/dummy/bin/bundle
66
- - test/dummy/bin/rails
67
- - test/dummy/bin/rake
68
- - test/dummy/bin/setup
69
- - test/dummy/config.ru
70
- - test/dummy/config/application.rb
71
- - test/dummy/config/boot.rb
72
- - test/dummy/config/database.yml
73
- - test/dummy/config/environment.rb
74
- - test/dummy/config/environments/development.rb
75
- - test/dummy/config/environments/production.rb
76
- - test/dummy/config/environments/test.rb
77
- - test/dummy/config/initializers/assets.rb
78
- - test/dummy/config/initializers/backtrace_silencers.rb
79
- - test/dummy/config/initializers/cookies_serializer.rb
80
- - test/dummy/config/initializers/filter_parameter_logging.rb
81
- - test/dummy/config/initializers/inflections.rb
82
- - test/dummy/config/initializers/mime_types.rb
83
- - test/dummy/config/initializers/session_store.rb
84
- - test/dummy/config/initializers/wrap_parameters.rb
85
- - test/dummy/config/locales/en.yml
86
- - test/dummy/config/routes.rb
87
- - test/dummy/config/secrets.yml
88
- - test/dummy/db/development.sqlite3
89
- - test/dummy/db/migrate/20151004115649_create_users.rb
90
- - test/dummy/db/migrate/20151004120032_create_posts.rb
91
- - test/dummy/db/migrate/20151004124603_create_categories.rb
92
- - test/dummy/db/migrate/20151004131127_categories_posts.rb
93
- - test/dummy/db/schema.rb
94
- - test/dummy/db/test.sqlite3
95
- - test/dummy/log/development.log
96
- - test/dummy/log/test.log
97
- - test/dummy/public/404.html
98
- - test/dummy/public/422.html
99
- - test/dummy/public/500.html
100
- - test/dummy/public/favicon.ico
101
- - test/dummy/test/fixtures/posts.yml
102
- - test/dummy/test/fixtures/users.yml
103
- - test/dummy/test/models/category_test.rb
104
- - test/dummy/test/models/post_test.rb
105
- - test/dummy/test/models/user_test.rb
106
- - test/factories.rb
107
- - test/nested_attr_test.rb
108
- - test/test_helper.rb
109
83
  homepage: https://github.com/sadaf2605/nested_attr
110
84
  licenses:
111
85
  - MIT
@@ -131,58 +105,4 @@ signing_key:
131
105
  specification_version: 4
132
106
  summary: Using this plugin you can use Factory Girl to test your formastic forms like
133
107
  that of active admin without much hassle.
134
- test_files:
135
- - test/nested_attr_test.rb
136
- - test/factories.rb
137
- - test/dummy/public/404.html
138
- - test/dummy/public/favicon.ico
139
- - test/dummy/public/500.html
140
- - test/dummy/public/422.html
141
- - test/dummy/config/database.yml
142
- - test/dummy/config/secrets.yml
143
- - test/dummy/config/environment.rb
144
- - test/dummy/config/initializers/backtrace_silencers.rb
145
- - test/dummy/config/initializers/session_store.rb
146
- - test/dummy/config/initializers/filter_parameter_logging.rb
147
- - test/dummy/config/initializers/assets.rb
148
- - test/dummy/config/initializers/mime_types.rb
149
- - test/dummy/config/initializers/cookies_serializer.rb
150
- - test/dummy/config/initializers/wrap_parameters.rb
151
- - test/dummy/config/initializers/inflections.rb
152
- - test/dummy/config/environments/test.rb
153
- - test/dummy/config/environments/development.rb
154
- - test/dummy/config/environments/production.rb
155
- - test/dummy/config/locales/en.yml
156
- - test/dummy/config/boot.rb
157
- - test/dummy/config/application.rb
158
- - test/dummy/config/routes.rb
159
- - test/dummy/test/models/category_test.rb
160
- - test/dummy/test/models/user_test.rb
161
- - test/dummy/test/models/post_test.rb
162
- - test/dummy/test/fixtures/posts.yml
163
- - test/dummy/test/fixtures/users.yml
164
- - test/dummy/Rakefile
165
- - test/dummy/README.rdoc
166
- - test/dummy/log/test.log
167
- - test/dummy/log/development.log
168
- - test/dummy/bin/rails
169
- - test/dummy/bin/bundle
170
- - test/dummy/bin/setup
171
- - test/dummy/bin/rake
172
- - test/dummy/app/views/layouts/application.html.erb
173
- - test/dummy/app/controllers/application_controller.rb
174
- - test/dummy/app/helpers/application_helper.rb
175
- - test/dummy/app/models/category.rb
176
- - test/dummy/app/models/post.rb
177
- - test/dummy/app/models/user.rb
178
- - test/dummy/app/assets/javascripts/application.js
179
- - test/dummy/app/assets/stylesheets/application.css
180
- - test/dummy/config.ru
181
- - test/dummy/db/schema.rb
182
- - test/dummy/db/test.sqlite3
183
- - test/dummy/db/development.sqlite3
184
- - test/dummy/db/migrate/20151004115649_create_users.rb
185
- - test/dummy/db/migrate/20151004120032_create_posts.rb
186
- - test/dummy/db/migrate/20151004124603_create_categories.rb
187
- - test/dummy/db/migrate/20151004131127_categories_posts.rb
188
- - test/test_helper.rb
108
+ test_files: []
@@ -1,28 +0,0 @@
1
- == README
2
-
3
- This README would normally document whatever steps are necessary to get the
4
- application up and running.
5
-
6
- Things you may want to cover:
7
-
8
- * Ruby version
9
-
10
- * System dependencies
11
-
12
- * Configuration
13
-
14
- * Database creation
15
-
16
- * Database initialization
17
-
18
- * How to run the test suite
19
-
20
- * Services (job queues, cache servers, search engines, etc.)
21
-
22
- * Deployment instructions
23
-
24
- * ...
25
-
26
-
27
- Please feel free to use a different markup language if you do not plan to run
28
- <tt>rake doc:app</tt>.
data/test/dummy/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- # Add your own tasks in files placed in lib/tasks ending in .rake,
2
- # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
-
4
- require File.expand_path('../config/application', __FILE__)
5
-
6
- Rails.application.load_tasks
@@ -1,13 +0,0 @@
1
- // This is a manifest file that'll be compiled into application.js, which will include all the files
2
- // listed below.
3
- //
4
- // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
- // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
- //
7
- // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
- // compiled file.
9
- //
10
- // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
- // about supported directives.
12
- //
13
- //= require_tree .
@@ -1,15 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any styles
10
- * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
- * file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */