cancan_strong_parameters 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
2
+ $: << File.dirname(__FILE__)
3
3
 
4
+ require "bundler/gem_tasks"
4
5
  require 'rake/testtask'
5
-
6
- Rake::TestTask.new do |t|
7
- t.libs << 'test'
8
- end
6
+ require 'test/rails_helper'
9
7
 
10
8
  desc "Run tests"
11
- task :default => :test
9
+ task :default => :test
10
+
11
+ Rails.application.load_tasks
@@ -1,3 +1,4 @@
1
+ require "active_support/hash_with_indifferent_access"
1
2
  require "cancan_strong_parameters/version"
2
3
  require "cancan_strong_parameters/controller"
3
4
  require "cancan_strong_parameters/deep_permit"
@@ -94,8 +94,12 @@ module CancanStrongParameters
94
94
  end
95
95
  end
96
96
 
97
- def resource_name
98
- self.to_s.sub("Controller", "").underscore.split('/').last.singularize
97
+ def resource_name(name_to_set=nil)
98
+ unless name_to_set.present?
99
+ @resource_name ||= self.to_s.sub("Controller", "").underscore.split('/').last.singularize
100
+ else
101
+ @resource_name = name_to_set
102
+ end
99
103
  end
100
104
  end
101
105
 
@@ -1,3 +1,3 @@
1
1
  module CancanStrongParameters
2
- VERSION = "0.2"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -11,6 +11,14 @@ class PostsController < ActionController::Base
11
11
  @post_attributes = params[:post]
12
12
  render json: @post
13
13
  end
14
- alias_method :update, :create
14
+
15
+ def update
16
+ @post = Post.find(params[:id])
17
+ if @post.update_attributes(params[:post])
18
+ render json: @post
19
+ else
20
+ render json: { errors: @post.errors.full_messages }, status: :unprocessable_entity
21
+ end
22
+ end
15
23
 
16
24
  end
@@ -0,0 +1,5 @@
1
+ class TitleController < PostsController
2
+ resource_name :post
3
+
4
+ permit_params :title
5
+ end
@@ -3,9 +3,26 @@ class Post
3
3
  include ActiveModel::MassAssignmentSecurity
4
4
  include ActiveModel::AttributeMethods
5
5
 
6
- attr_accessible :body, :content
6
+ attr_accessor :title, :body
7
+ attr_accessible :title, :body
7
8
 
8
9
  def initialize(attributes = {})
9
10
  @attributes = attributes
10
11
  end
12
+
13
+ # Fake persistence
14
+ def self.sample_post
15
+ new(title: "Sample post", body: "Sample body")
16
+ end
17
+
18
+ def self.find(*args)
19
+ sample_post
20
+ end
21
+
22
+ def update_attributes(params)
23
+ params.map do |k,v|
24
+ setter = :"#{k}=" # setter
25
+ self.send(setter, v) if self.respond_to?(setter)
26
+ end
27
+ end
11
28
  end
@@ -1,13 +1,4 @@
1
- require 'minitest/autorun'
2
-
3
- require 'require_all'
4
-
5
- require 'strong_parameters'
6
- require 'cancan_strong_parameters'
7
-
8
- ## Boot up an instance of rails
9
- require 'rails_helper'
10
- require 'rails/test_help'
1
+ require 'test_helper'
11
2
 
12
3
  class PostsControllerTest < ActionController::TestCase
13
4
  test "should not clip off deep params" do
@@ -37,7 +28,7 @@ class PostsControllerTest < ActionController::TestCase
37
28
  }
38
29
  }
39
30
 
40
- put :update, {id: 1}.merge(params)
31
+ post :create, params
41
32
  assert_equal \
42
33
  ActiveSupport::HashWithIndifferentAccess.new(assigns(:post_attributes)),
43
34
  ActiveSupport::HashWithIndifferentAccess.new(params[:post])
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ class TitleControllerTest < ActionController::TestCase
4
+
5
+ test "can manually change resource_name" do
6
+
7
+ new_title = 'Changed title'
8
+ put :update,
9
+ id: 1,
10
+ post: {
11
+ title: new_title
12
+ }
13
+
14
+ assert_equal assigns[:post].title, new_title
15
+ end
16
+
17
+ end
data/test/rails_helper.rb CHANGED
@@ -34,4 +34,6 @@ Config::Application.initialize!
34
34
 
35
35
  Config::Application.routes.draw do
36
36
  resources :posts
37
- end
37
+
38
+ match 'title/:id', to: 'title#update', via: :put
39
+ end
@@ -0,0 +1,10 @@
1
+ require 'require_all'
2
+
3
+ require 'minitest/autorun'
4
+
5
+ require 'strong_parameters'
6
+ require 'cancan_strong_parameters'
7
+
8
+ ## Boot up an instance of rails
9
+ require 'rails_helper'
10
+ require 'rails/test_help'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cancan_strong_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-30 00:00:00.000000000 Z
12
+ date: 2012-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cancan
@@ -142,11 +142,14 @@ files:
142
142
  - lib/cancan_strong_parameters/rails/controller/base.rb
143
143
  - lib/cancan_strong_parameters/version.rb
144
144
  - test/app/controllers/posts_controller.rb
145
+ - test/app/controllers/title_controller.rb
145
146
  - test/app/models/post.rb
146
147
  - test/config.ru
148
+ - test/functional/posts_controller_test.rb
149
+ - test/functional/title_controller_test.rb
147
150
  - test/rails_helper.rb
148
151
  - test/script/rails
149
- - test/test_cancan_strong_parameters.rb
152
+ - test/test_helper.rb
150
153
  homepage: https://github.com/colinyoung/cancan_strong_parameters
151
154
  licenses: []
152
155
  post_install_message:
@@ -173,9 +176,11 @@ specification_version: 3
173
176
  summary: make CanCan work with strong_parameters
174
177
  test_files:
175
178
  - test/app/controllers/posts_controller.rb
179
+ - test/app/controllers/title_controller.rb
176
180
  - test/app/models/post.rb
177
181
  - test/config.ru
182
+ - test/functional/posts_controller_test.rb
183
+ - test/functional/title_controller_test.rb
178
184
  - test/rails_helper.rb
179
185
  - test/script/rails
180
- - test/test_cancan_strong_parameters.rb
181
- has_rdoc:
186
+ - test/test_helper.rb