dynamometer 0.0.8 → 0.0.9

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 921d2fe3bb72e407fb99012ded3142f80d25ba28
4
- data.tar.gz: 7419a085efb613ef2a5f78b22bdd450afb9f97d5
3
+ metadata.gz: 665f74be9bfc57b0ccf3f9f13fca0c2b86d4125c
4
+ data.tar.gz: cbbb7cad8c5b392254394074b37a202641b0dc26
5
5
  SHA512:
6
- metadata.gz: 988d4cef8655ad8c5524eb646fe4f77f9e9b3fea24250bc48d6fb00966364f7114b498b056f0e3be63486ece12a7e3b7edc22e9e8936085c38cb8c924efe42ed
7
- data.tar.gz: ea42b47cfb048c87f34aec18ed908045cb5518e3801e8e5960a387cef5a1ddbbd656d3db0e15bea3c0577a0d2c571f8c3581b3da7f1f15d407954d0b49652f3d
6
+ metadata.gz: 1e5019445cade3d6f922a701c73620e310bd688607b90ba88f7d1888247133bda2bdeceedffd90db03e6d5445acd7238c8c4cbeea11a38627f0cf2bcb8776387
7
+ data.tar.gz: 837cbb0ae2180d18894e61a2f8afb0477242408aaaa387433f09ab2511199b9b31d8f8a97ef14c469e28e8955e10b3468034345aff0bc2af6a6ae37f3bf32622
data/README.md CHANGED
@@ -90,6 +90,29 @@ If you want to serialize all of your dynamic attributes using activemodel serial
90
90
  attributes :id
91
91
  end
92
92
 
93
+ ## Strong Parameters
94
+
95
+ To specify that dynamic attributes should be allowed using strong parameters,
96
+ include the `PermitDynamic` concern in your controller and specify the model
97
+ to be checked against.
98
+
99
+ class PeopleController < ApplicationController
100
+ include PermitDynamic
101
+
102
+ def create
103
+ @person = Person.create(person_params)
104
+ render json: @person
105
+ end
106
+
107
+ private
108
+
109
+ def person_params
110
+ params.require(:person).permit(:name, dynamic_attributes: Person)
111
+ end
112
+ end
113
+
114
+ This will permit any parameters that are NOT valid regular attributes of `Person`.
115
+
93
116
  ## Installation
94
117
 
95
118
  Add this line to your application's Gemfile:
@@ -0,0 +1,19 @@
1
+ require 'dynamometer/parameters'
2
+
3
+ module PermitDynamic
4
+ extend ActiveSupport::Concern
5
+
6
+ # extend ActionController::Parameters to allow dynamic attributes
7
+
8
+ def params
9
+ @_params ||= Dynamometer::Parameters.new(request.parameters)
10
+ end
11
+
12
+ # Assigns the given +value+ to the +params+ hash. If +value+
13
+ # is a Hash, this will create an ActionController::Parameters
14
+ # object that has been instantiated with the given +value+ hash.
15
+ def params=(value)
16
+ @_params = value.is_a?(Hash) ? Dynamometer::Parameters.new(value) : value
17
+ end
18
+
19
+ end
@@ -10,6 +10,9 @@ module DynamicAttributes
10
10
  end
11
11
 
12
12
  def dynamic_attributes(*args)
13
+ @_dynamic_attributes ||= []
14
+ @_dynamic_attributes |= args.map(&:to_s)
15
+
13
16
  args.each do |attr|
14
17
  class_eval <<-ENDOFCODE
15
18
  def #{attr}
@@ -22,6 +25,12 @@ module DynamicAttributes
22
25
  ENDOFCODE
23
26
  end
24
27
  end
28
+
29
+ def permitted_dynamic_attribute?(attr)
30
+ # make sure the regular attributes have been defined
31
+ define_attribute_methods
32
+ @_dynamic_attributes.include?(attr.to_s) || instance_methods.none? { |m| m.to_s == attr.to_s }
33
+ end
25
34
  end
26
35
 
27
36
  def [](attr_name)
@@ -0,0 +1,20 @@
1
+ module Dynamometer
2
+ class Parameters < ActionController::Parameters
3
+
4
+ def hash_filter(params, filter)
5
+ filter = filter.with_indifferent_access
6
+ # this is tricky - we grab the :dynamic_attributes key from params
7
+ # and use it to check the rest of our keys
8
+ #
9
+ if filter.has_key?('dynamic_attributes')
10
+ model = filter.delete('dynamic_attributes')
11
+ self.keys.each do |key|
12
+ # if the key is already in params it's OK
13
+ next if params[key] || !model.permitted_dynamic_attribute?(key)
14
+ permitted_scalar_filter(params, key)
15
+ end
16
+ end
17
+ super(params, filter)
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Dynamometer
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -0,0 +1,18 @@
1
+ class PeopleController < ApplicationController
2
+ include PermitDynamic
3
+
4
+ def create
5
+ @person = Person.create(person_params)
6
+ render json: @person
7
+ end
8
+
9
+ rescue_from 'ActionController::UnpermittedParameters' do |ex|
10
+ render json: { error: 'unpermitted_parameters' }, status: :bad_request
11
+ end
12
+
13
+ private
14
+
15
+ def person_params
16
+ params.require(:person).permit(:name, :dynamic_attributes => Person)
17
+ end
18
+ end
@@ -1,6 +1,8 @@
1
1
  Dummy::Application.configure do
2
2
  # Settings specified here will take precedence over those in config/application.rb.
3
3
 
4
+ config.action_controller.action_on_unpermitted_parameters = :raise
5
+
4
6
  # The test environment is used exclusively to run your application's
5
7
  # test suite. You never need to work with it otherwise. Remember that
6
8
  # your test database is "scratch space" for the test suite and is wiped
@@ -1,56 +1,3 @@
1
1
  Dummy::Application.routes.draw do
2
- # The priority is based upon order of creation: first created -> highest priority.
3
- # See how all your routes lay out with "rake routes".
4
-
5
- # You can have the root of your site routed with "root"
6
- # root 'welcome#index'
7
-
8
- # Example of regular route:
9
- # get 'products/:id' => 'catalog#view'
10
-
11
- # Example of named route that can be invoked with purchase_url(id: product.id)
12
- # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
13
-
14
- # Example resource route (maps HTTP verbs to controller actions automatically):
15
- # resources :products
16
-
17
- # Example resource route with options:
18
- # resources :products do
19
- # member do
20
- # get 'short'
21
- # post 'toggle'
22
- # end
23
- #
24
- # collection do
25
- # get 'sold'
26
- # end
27
- # end
28
-
29
- # Example resource route with sub-resources:
30
- # resources :products do
31
- # resources :comments, :sales
32
- # resource :seller
33
- # end
34
-
35
- # Example resource route with more complex sub-resources:
36
- # resources :products do
37
- # resources :comments
38
- # resources :sales do
39
- # get 'recent', on: :collection
40
- # end
41
- # end
42
-
43
- # Example resource route with concerns:
44
- # concern :toggleable do
45
- # post 'toggle'
46
- # end
47
- # resources :posts, concerns: :toggleable
48
- # resources :photos, concerns: :toggleable
49
-
50
- # Example resource route within a namespace:
51
- # namespace :admin do
52
- # # Directs /admin/products/* to Admin::ProductsController
53
- # # (app/controllers/admin/products_controller.rb)
54
- # resources :products
55
- # end
2
+ resources :people
56
3
  end
@@ -0,0 +1,33 @@
1
+ class PeopleControllerTest < ActionController::TestCase
2
+
3
+ tests PeopleController
4
+
5
+ test "create without dynamic attributes" do
6
+ post :create, person: { name: "Nobody" }
7
+
8
+ assert_response :success
9
+ assert Person.find_by(name: 'Nobody').present?
10
+ end
11
+
12
+ test "create with declared dynamic attributes" do
13
+ post :create, person: { name: "Nobody", hometown: "Nowhere" }
14
+
15
+ assert_response :success
16
+ assert Person.find_by(name: 'Nobody').present?
17
+ end
18
+
19
+ test "create with arbitrary dynamic attributes" do
20
+ post :create, person: { name: "Nobody", magic_level: "over 9000" }
21
+
22
+ assert_response :success
23
+ assert Person.find_by(name: 'Nobody').present?
24
+ end
25
+
26
+ test "create with valid but forbidden attributes fails" do
27
+ post :create, person: { name: "Nobody", father_id: 17 }
28
+
29
+ assert_response :bad_request
30
+ end
31
+
32
+ end
33
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamometer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Colvin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-11 00:00:00.000000000 Z
11
+ date: 2013-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -64,11 +64,13 @@ files:
64
64
  - LICENSE.txt
65
65
  - README.md
66
66
  - Rakefile
67
+ - app/controllers/concerns/permit_dynamic.rb
67
68
  - app/models/concerns/dynamic_attributes.rb
68
69
  - app/serializers/dynamic_attributes_serializer.rb
69
70
  - dynamometer.gemspec
70
71
  - lib/dynamometer.rb
71
72
  - lib/dynamometer/dynamic_attributes_in_where.rb
73
+ - lib/dynamometer/parameters.rb
72
74
  - lib/dynamometer/railtie.rb
73
75
  - lib/dynamometer/version.rb
74
76
  - lib/tasks/dynamometer_tasks.rake
@@ -79,6 +81,7 @@ files:
79
81
  - test/dummy/app/assets/stylesheets/application.css
80
82
  - test/dummy/app/controllers/application_controller.rb
81
83
  - test/dummy/app/controllers/concerns/.keep
84
+ - test/dummy/app/controllers/people_controller.rb
82
85
  - test/dummy/app/helpers/application_helper.rb
83
86
  - test/dummy/app/mailers/.keep
84
87
  - test/dummy/app/models/.keep
@@ -117,6 +120,7 @@ files:
117
120
  - test/dummy/public/500.html
118
121
  - test/dummy/public/favicon.ico
119
122
  - test/dynamometer_test.rb
123
+ - test/people_controller_test.rb
120
124
  - test/person_test.rb
121
125
  - test/serializer_test.rb
122
126
  - test/test_helper.rb
@@ -152,6 +156,7 @@ test_files:
152
156
  - test/dummy/app/assets/stylesheets/application.css
153
157
  - test/dummy/app/controllers/application_controller.rb
154
158
  - test/dummy/app/controllers/concerns/.keep
159
+ - test/dummy/app/controllers/people_controller.rb
155
160
  - test/dummy/app/helpers/application_helper.rb
156
161
  - test/dummy/app/mailers/.keep
157
162
  - test/dummy/app/models/.keep
@@ -190,6 +195,7 @@ test_files:
190
195
  - test/dummy/public/500.html
191
196
  - test/dummy/public/favicon.ico
192
197
  - test/dynamometer_test.rb
198
+ - test/people_controller_test.rb
193
199
  - test/person_test.rb
194
200
  - test/serializer_test.rb
195
201
  - test/test_helper.rb