attr_accessible_block 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +4 -0
- data/Gemfile +3 -9
- data/Gemfile.lock +28 -31
- data/README.md +13 -9
- data/Rakefile +5 -49
- data/attr_accessible_block.gemspec +16 -70
- data/lib/attr_accessible_block.rb +64 -58
- data/lib/attr_accessible_block/version.rb +3 -0
- data/spec/attr_accessible_block_spec.rb +93 -0
- data/spec/spec_helper.rb +2 -0
- metadata +57 -155
- data/init.rb +0 -1
- data/install.rb +0 -1
- data/tasks/attr_accessible_block_tasks.rake +0 -4
- data/test/schema.rb +0 -58
- data/test/test_attr_accessible_block.rb +0 -88
- data/test/test_helper.rb +0 -24
- data/uninstall.rb +0 -1
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -1,11 +1,5 @@
|
|
1
|
-
source
|
1
|
+
source 'http://rubygems.org'
|
2
2
|
|
3
|
-
gem
|
3
|
+
# Specify your gem's dependencies in attr_accessible_block.gemspec
|
4
|
+
gemspec
|
4
5
|
|
5
|
-
group :development do
|
6
|
-
gem "shoulda", ">= 0"
|
7
|
-
gem "bundler", "~> 1.0.0"
|
8
|
-
gem "jeweler", "~> 1.5.2"
|
9
|
-
gem 'sqlite3-ruby', '~> 1.3.2'
|
10
|
-
gem "rcov", ">= 0"
|
11
|
-
end
|
data/Gemfile.lock
CHANGED
@@ -1,39 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
attr_accessible_block (0.3.0)
|
5
|
+
activemodel (>= 3.1)
|
6
|
+
|
1
7
|
GEM
|
2
8
|
remote: http://rubygems.org/
|
3
9
|
specs:
|
4
|
-
activemodel (3.
|
5
|
-
activesupport (= 3.
|
6
|
-
builder (~>
|
7
|
-
i18n (~> 0.
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
rcov (0.9.9)
|
24
|
-
shoulda (2.11.3)
|
25
|
-
sqlite3 (1.3.3)
|
26
|
-
sqlite3-ruby (1.3.3)
|
27
|
-
sqlite3 (>= 1.3.3)
|
28
|
-
tzinfo (0.3.27)
|
10
|
+
activemodel (3.1.3)
|
11
|
+
activesupport (= 3.1.3)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
activesupport (3.1.3)
|
15
|
+
multi_json (~> 1.0)
|
16
|
+
builder (3.0.0)
|
17
|
+
diff-lcs (1.1.3)
|
18
|
+
i18n (0.6.0)
|
19
|
+
multi_json (1.0.3)
|
20
|
+
rake (0.9.2.2)
|
21
|
+
rspec (2.6.0)
|
22
|
+
rspec-core (~> 2.6.0)
|
23
|
+
rspec-expectations (~> 2.6.0)
|
24
|
+
rspec-mocks (~> 2.6.0)
|
25
|
+
rspec-core (2.6.4)
|
26
|
+
rspec-expectations (2.6.0)
|
27
|
+
diff-lcs (~> 1.1.2)
|
28
|
+
rspec-mocks (2.6.0)
|
29
29
|
|
30
30
|
PLATFORMS
|
31
31
|
ruby
|
32
32
|
|
33
33
|
DEPENDENCIES
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
rcov
|
38
|
-
shoulda
|
39
|
-
sqlite3-ruby (~> 1.3.2)
|
34
|
+
attr_accessible_block!
|
35
|
+
rake
|
36
|
+
rspec
|
data/README.md
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
AttrAccessibleBlock 0.
|
1
|
+
AttrAccessibleBlock 0.3.0
|
2
2
|
=========================
|
3
3
|
|
4
|
-
|
4
|
+
[![travis-ci status](https://secure.travis-ci.org/dmitry/attr_accessible_block.png)](http://travis-ci.org/dmitry/attr_accessible_block)
|
5
5
|
|
6
|
-
|
6
|
+
> If you need same functionallity for the Rails 2.3 or Rails 3.0, then use v0.2.2, it's fully tested and ready for this oldies. New version is total rewrite of the previous plugin, but API is the same.
|
7
7
|
|
8
|
-
|
8
|
+
This is an ActiveModel plugin with possibility to define block inside the `attr_accessible` class method.
|
9
|
+
|
10
|
+
Because of block, it's possible to define accessibles for instances, not just for the class level.
|
9
11
|
|
10
12
|
It's also still possible to define class level accessibles, so an old `attr_accessible :name` will work.
|
11
13
|
|
@@ -62,14 +64,14 @@ And on user update changing of email will be rejected because of `new_record?` m
|
|
62
64
|
|
63
65
|
Sometimes you may need to check is attribute of model assignable or no (this method mostly interesting when doing form inputs). You can do it with using `attr_accessible?` method:
|
64
66
|
|
65
|
-
user.
|
66
|
-
user.
|
67
|
+
user.attr_accessible?(:email) # returns false
|
68
|
+
user.attr_accessible?(:password) # returns true
|
67
69
|
|
68
70
|
How do I add something similar to `record`, for example I want to check current users role?
|
69
71
|
|
70
72
|
Easy, with `sentient_user` gem and add the code to the `config/initializers/plugins.rb` file:
|
71
73
|
|
72
|
-
|
74
|
+
ActiveModel::MassAssignmentSecurity::WhiteListBlock.add_variable(:user) { User.current || User.new }
|
73
75
|
|
74
76
|
Now `user` method available, you can check:
|
75
77
|
|
@@ -83,7 +85,7 @@ What if I want to provide an total accessibility for the admin user?
|
|
83
85
|
|
84
86
|
Just add this code to the `config/initializers/plugins.rb` file:
|
85
87
|
|
86
|
-
|
88
|
+
ActiveModel::MassAssignmentSecurity::WhiteListBlock.always_accessible { user.admin? }
|
87
89
|
|
88
90
|
NOTICE: when using attr_accessible as a block, then no second parameter is available for the `attributes=` method (guard_protected_attributes = true). Instead use power of blocks! Also do not use attr_protected, because it's bad :)
|
89
91
|
|
@@ -91,4 +93,6 @@ Should be STI compatible, but haven't tested yet. Need's feedback on this featur
|
|
91
93
|
|
92
94
|
For more answers on your questions you can look into tests and source code.
|
93
95
|
|
94
|
-
|
96
|
+
Used on http://tenerife.by
|
97
|
+
|
98
|
+
Copyright (c) 2012 Dmitry Polushkin, released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,50 +1,6 @@
|
|
1
|
-
|
2
|
-
require 'bundler'
|
3
|
-
|
4
|
-
Bundler.setup(:default, :development)
|
5
|
-
rescue Bundler::BundlerError => e
|
6
|
-
$stderr.puts e.message
|
7
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
-
exit e.status_code
|
9
|
-
end
|
10
|
-
require 'rake'
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rspec/core/rake_task'
|
11
4
|
|
12
|
-
|
13
|
-
|
14
|
-
gem.name = "attr_accessible_block"
|
15
|
-
gem.homepage = "http://github.com/dmitry/attr_accessible_block"
|
16
|
-
gem.license = "MIT"
|
17
|
-
gem.summary = %Q{Attribute accessible block (attr_accessible with a dynamic block possibility)}
|
18
|
-
gem.description = %Q{Convinient possibility to change attr_accessible on the fly, using definition of the required accessible attributes in a block.}
|
19
|
-
gem.email = "dmitry.polushkin@gmail.com"
|
20
|
-
gem.authors = ["Dmitry Polushkin"]
|
21
|
-
gem.version = '0.2.2'
|
22
|
-
gem.add_runtime_dependency 'activerecord', '>= 2.3.5'
|
23
|
-
end
|
24
|
-
Jeweler::RubygemsDotOrgTasks.new
|
25
|
-
|
26
|
-
require 'rake/testtask'
|
27
|
-
Rake::TestTask.new(:test) do |test|
|
28
|
-
test.libs << 'lib' << 'test'
|
29
|
-
test.pattern = 'test/**/test_*.rb'
|
30
|
-
test.verbose = true
|
31
|
-
end
|
32
|
-
|
33
|
-
require 'rcov/rcovtask'
|
34
|
-
Rcov::RcovTask.new do |test|
|
35
|
-
test.libs << 'test'
|
36
|
-
test.pattern = 'test/**/test_*.rb'
|
37
|
-
test.verbose = true
|
38
|
-
end
|
39
|
-
|
40
|
-
task :default => :test
|
41
|
-
|
42
|
-
require 'rake/rdoctask'
|
43
|
-
Rake::RDocTask.new do |rdoc|
|
44
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
-
|
46
|
-
rdoc.rdoc_dir = 'rdoc'
|
47
|
-
rdoc.title = "attr_accessible_block #{version}"
|
48
|
-
rdoc.rdoc_files.include('README*')
|
49
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
-
end
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
task :default => :spec
|
@@ -1,75 +1,21 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/attr_accessible_block/version', __FILE__)
|
5
3
|
|
6
|
-
Gem::Specification.new do |
|
7
|
-
|
8
|
-
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Dmitry Polushkin"]
|
6
|
+
gem.email = ["dmitry.polushkin@gmail.com"]
|
7
|
+
gem.description = %q{Set attr_accessible attributes on runtime.}
|
8
|
+
gem.summary = %q{Bonus power attr_accessible on steroids with possibility to change accessibles on the fly.}
|
9
|
+
gem.homepage = "https://github.com/dmitry/attr_accessible_block"
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
"README.md"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
"Gemfile",
|
20
|
-
"Gemfile.lock",
|
21
|
-
"MIT-LICENSE",
|
22
|
-
"README.md",
|
23
|
-
"Rakefile",
|
24
|
-
"attr_accessible_block.gemspec",
|
25
|
-
"init.rb",
|
26
|
-
"install.rb",
|
27
|
-
"lib/attr_accessible_block.rb",
|
28
|
-
"tasks/attr_accessible_block_tasks.rake",
|
29
|
-
"test/schema.rb",
|
30
|
-
"test/test_attr_accessible_block.rb",
|
31
|
-
"test/test_helper.rb",
|
32
|
-
"uninstall.rb"
|
33
|
-
]
|
34
|
-
s.homepage = %q{http://github.com/dmitry/attr_accessible_block}
|
35
|
-
s.licenses = ["MIT"]
|
36
|
-
s.require_paths = ["lib"]
|
37
|
-
s.rubygems_version = %q{1.6.2}
|
38
|
-
s.summary = %q{Attribute accessible block (attr_accessible with a dynamic block possibility)}
|
39
|
-
s.test_files = [
|
40
|
-
"test/schema.rb",
|
41
|
-
"test/test_attr_accessible_block.rb",
|
42
|
-
"test/test_helper.rb"
|
43
|
-
]
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "attr_accessible_block"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = AttrAccessibleBlock::VERSION
|
44
17
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
-
s.add_runtime_dependency(%q<activerecord>, [">= 2.3.5"])
|
50
|
-
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
51
|
-
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
52
|
-
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
53
|
-
s.add_development_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
54
|
-
s.add_development_dependency(%q<rcov>, [">= 0"])
|
55
|
-
s.add_runtime_dependency(%q<activerecord>, [">= 2.3.5"])
|
56
|
-
else
|
57
|
-
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
58
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
59
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
60
|
-
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
61
|
-
s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
62
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
63
|
-
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
64
|
-
end
|
65
|
-
else
|
66
|
-
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
67
|
-
s.add_dependency(%q<shoulda>, [">= 0"])
|
68
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
69
|
-
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
70
|
-
s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.2"])
|
71
|
-
s.add_dependency(%q<rcov>, [">= 0"])
|
72
|
-
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
73
|
-
end
|
18
|
+
gem.add_dependency "activemodel", '>= 3.1'
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
gem.add_development_dependency "rake"
|
74
21
|
end
|
75
|
-
|
@@ -1,81 +1,87 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require "attr_accessible_block/version"
|
2
|
+
|
3
|
+
module ActiveModel::MassAssignmentSecurity
|
4
|
+
module ClassMethods
|
3
5
|
alias_method :old_attr_accessible, :attr_accessible
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
|
7
|
+
def attr_accessible(*attributes, &block)
|
8
|
+
if block_given?
|
9
|
+
class_attribute(:attr_accessible_block)
|
10
|
+
self.attr_accessible_block = block
|
11
|
+
|
12
|
+
include InstanceMethods
|
13
|
+
else
|
14
|
+
old_attr_accessible(*attributes)
|
13
15
|
end
|
14
|
-
else
|
15
|
-
old_attr_accessible(*attributes)
|
16
16
|
end
|
17
|
-
end
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
# rails 2/3 compatibility
|
28
|
-
if klass.respond_to?(:accessible_attributes)
|
29
|
-
klass.accessible_attributes.include?(attribute.to_s)
|
30
|
-
else
|
31
|
-
klass._accessible_attributes.include?(attribute)
|
18
|
+
module InstanceMethods
|
19
|
+
def sanitize_for_mass_assignment(attributes, role = :default)
|
20
|
+
mass_assignment_authorizer.sanitize(attributes, self)
|
21
|
+
end
|
22
|
+
|
23
|
+
def mass_assignment_authorizer
|
24
|
+
WhiteListBlock.new(&self.class.attr_accessible_block)
|
32
25
|
end
|
33
26
|
end
|
34
27
|
end
|
35
|
-
end
|
36
28
|
|
29
|
+
def attr_accessible?(attribute)
|
30
|
+
block = self.class.attr_accessible_block
|
31
|
+
attributes = WhiteListBlock.new(&block).sanitize({attribute => send(attribute)}, self)
|
32
|
+
attributes.has_key?(attribute)
|
33
|
+
end
|
37
34
|
|
38
|
-
class ActiveRecord::AttrAccessibleBlock < Array
|
39
|
-
attr_reader :attrs, :record
|
40
35
|
|
41
|
-
|
42
|
-
|
36
|
+
class WhiteListBlock < PermissionSet
|
37
|
+
attr_reader :attributes, :record
|
43
38
|
|
44
|
-
|
45
|
-
|
39
|
+
@@variables = {}
|
40
|
+
@@always_accessible = nil
|
46
41
|
|
47
|
-
|
48
|
-
|
49
|
-
end
|
42
|
+
def sanitize(attributes, record)
|
43
|
+
@attributes = attributes
|
50
44
|
|
51
|
-
|
45
|
+
@@variables.each do |name, func|
|
46
|
+
instance_variable_set("@#{name}", func.call)
|
47
|
+
end
|
52
48
|
|
53
|
-
|
49
|
+
@record = record
|
54
50
|
|
55
|
-
|
56
|
-
instance_eval(&block)
|
51
|
+
always_accessible = (@@always_accessible ? instance_eval(&@@always_accessible) : false)
|
57
52
|
|
58
|
-
|
59
|
-
|
53
|
+
unless always_accessible
|
54
|
+
instance_eval(&@block)
|
55
|
+
|
56
|
+
flatten!
|
57
|
+
reject_attributes!
|
58
|
+
end
|
59
|
+
|
60
|
+
@attributes
|
60
61
|
end
|
61
|
-
end
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
def initialize(&block)
|
64
|
+
@block = block
|
65
|
+
super
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
68
|
+
def add(attributes)
|
69
|
+
Array.wrap(attributes).map(&:to_s).each { |attribute| super(attribute) }
|
70
|
+
end
|
71
71
|
|
72
|
-
|
73
|
-
|
74
|
-
|
72
|
+
def self.add_variable(name, &block)
|
73
|
+
@@variables[name] = block
|
74
|
+
attr_reader name
|
75
|
+
end
|
75
76
|
|
76
|
-
|
77
|
+
def self.always_accessible(&block)
|
78
|
+
@@always_accessible = block
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
77
82
|
|
78
|
-
|
79
|
-
|
83
|
+
def reject_attributes!
|
84
|
+
@attributes.reject! { |k,v| !include?(k) }
|
85
|
+
end
|
80
86
|
end
|
81
|
-
end
|
87
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
class BaseModel
|
5
|
+
include ActiveModel::MassAssignmentSecurity
|
6
|
+
include ActiveModel::AttributeMethods
|
7
|
+
|
8
|
+
def self.attributes(attributes)
|
9
|
+
attr_accessor *attributes
|
10
|
+
define_attribute_methods attributes
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(attributes={})
|
14
|
+
sanitize_for_mass_assignment(attributes).each do |name, value|
|
15
|
+
update_attribute(name, value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def update_attribute(name, value)
|
20
|
+
multi = (name.to_s.match(/^(.+)\(/))
|
21
|
+
name = multi[1] if multi
|
22
|
+
|
23
|
+
send(:"#{name}=", value)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class User < BaseModel
|
28
|
+
attributes %w(email password date role)
|
29
|
+
|
30
|
+
attr_accessible :email, :password, :date
|
31
|
+
|
32
|
+
def self.current
|
33
|
+
User.new
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Location < BaseModel
|
38
|
+
attributes %w(name size user point)
|
39
|
+
|
40
|
+
attr_accessible do
|
41
|
+
add [:name, :size]
|
42
|
+
add :user if user.role == :moderator
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
ActiveModel::MassAssignmentSecurity::WhiteListBlock.add_variable(:user) { User.current }
|
47
|
+
ActiveModel::MassAssignmentSecurity::WhiteListBlock.always_accessible { User.current.role == :admin }
|
48
|
+
|
49
|
+
describe AttrAccessibleBlock do
|
50
|
+
it "should have standard static attr_accessible" do
|
51
|
+
user = User.new(:email => 'test@test.com', :password => 'test', 'date(i0)' => '10', :role => 'admin')
|
52
|
+
user.password.should eq 'test'
|
53
|
+
user.email.should eq 'test@test.com'
|
54
|
+
user.date.should eq '10'
|
55
|
+
user.role.should be_nil
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should have block attr_accessible" do
|
59
|
+
location = Location.new(:name => 'test', :size => 10, :user => 'user', 'point(i2)' => '10')
|
60
|
+
location.name.should eq 'test'
|
61
|
+
location.size.should eq 10
|
62
|
+
location.user.should be_nil
|
63
|
+
location.point.should be_nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should have block attr_accessible" do
|
67
|
+
location = Location.new(:name => 'test', :size => 10, :user => 'user', 'point(i2)' => '10')
|
68
|
+
location.attr_accessible?(:name).should be_true
|
69
|
+
location.attr_accessible?(:size).should be_true
|
70
|
+
location.attr_accessible?(:user).should be_false
|
71
|
+
location.attr_accessible?(:point).should be_false
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should have block attr_accessible that always accessible" do
|
75
|
+
u = User.new.tap { |u| u.role = :admin }
|
76
|
+
User.should_receive(:current).twice.and_return(u)
|
77
|
+
location = Location.new(:name => 'test', :size => 10, :user => 'user', 'point(i2)' => '10')
|
78
|
+
location.name.should eq 'test'
|
79
|
+
location.size.should eq 10
|
80
|
+
location.user.should eq 'user'
|
81
|
+
location.point.should eq '10'
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should have block attr_accessible uses variable" do
|
85
|
+
u = User.new.tap { |u| u.role = :moderator }
|
86
|
+
User.should_receive(:current).twice.and_return(u)
|
87
|
+
location = Location.new(:name => 'test', :size => 10, :user => 'user', 'point(i2)' => '10')
|
88
|
+
location.name.should eq 'test'
|
89
|
+
location.size.should eq 10
|
90
|
+
location.user.should eq 'user'
|
91
|
+
location.point.should be_nil
|
92
|
+
end
|
93
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,189 +1,91 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_accessible_block
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
- 2
|
10
|
-
version: 0.2.2
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Dmitry Polushkin
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
name: activerecord
|
23
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
12
|
+
date: 2011-12-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: &18285840 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 2
|
31
|
-
- 3
|
32
|
-
- 5
|
33
|
-
version: 2.3.5
|
34
|
-
prerelease: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
35
22
|
type: :runtime
|
36
|
-
requirement: *id001
|
37
|
-
- !ruby/object:Gem::Dependency
|
38
|
-
name: shoulda
|
39
|
-
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
-
none: false
|
41
|
-
requirements:
|
42
|
-
- - ">="
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
hash: 3
|
45
|
-
segments:
|
46
|
-
- 0
|
47
|
-
version: "0"
|
48
23
|
prerelease: false
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
version_requirements: &id003 !ruby/object:Gem::Requirement
|
24
|
+
version_requirements: *18285840
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &18284400 !ruby/object:Gem::Requirement
|
54
28
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
segments:
|
60
|
-
- 1
|
61
|
-
- 0
|
62
|
-
- 0
|
63
|
-
version: 1.0.0
|
64
|
-
prerelease: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
65
33
|
type: :development
|
66
|
-
requirement: *id003
|
67
|
-
- !ruby/object:Gem::Dependency
|
68
|
-
name: jeweler
|
69
|
-
version_requirements: &id004 !ruby/object:Gem::Requirement
|
70
|
-
none: false
|
71
|
-
requirements:
|
72
|
-
- - ~>
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
hash: 7
|
75
|
-
segments:
|
76
|
-
- 1
|
77
|
-
- 5
|
78
|
-
- 2
|
79
|
-
version: 1.5.2
|
80
34
|
prerelease: false
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
version_requirements: &id005 !ruby/object:Gem::Requirement
|
35
|
+
version_requirements: *18284400
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &18282700 !ruby/object:Gem::Requirement
|
86
39
|
none: false
|
87
|
-
requirements:
|
88
|
-
- -
|
89
|
-
- !ruby/object:Gem::Version
|
90
|
-
|
91
|
-
segments:
|
92
|
-
- 1
|
93
|
-
- 3
|
94
|
-
- 2
|
95
|
-
version: 1.3.2
|
96
|
-
prerelease: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
97
44
|
type: :development
|
98
|
-
requirement: *id005
|
99
|
-
- !ruby/object:Gem::Dependency
|
100
|
-
name: rcov
|
101
|
-
version_requirements: &id006 !ruby/object:Gem::Requirement
|
102
|
-
none: false
|
103
|
-
requirements:
|
104
|
-
- - ">="
|
105
|
-
- !ruby/object:Gem::Version
|
106
|
-
hash: 3
|
107
|
-
segments:
|
108
|
-
- 0
|
109
|
-
version: "0"
|
110
|
-
prerelease: false
|
111
|
-
type: :development
|
112
|
-
requirement: *id006
|
113
|
-
- !ruby/object:Gem::Dependency
|
114
|
-
name: activerecord
|
115
|
-
version_requirements: &id007 !ruby/object:Gem::Requirement
|
116
|
-
none: false
|
117
|
-
requirements:
|
118
|
-
- - ">="
|
119
|
-
- !ruby/object:Gem::Version
|
120
|
-
hash: 9
|
121
|
-
segments:
|
122
|
-
- 2
|
123
|
-
- 3
|
124
|
-
- 5
|
125
|
-
version: 2.3.5
|
126
45
|
prerelease: false
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
46
|
+
version_requirements: *18282700
|
47
|
+
description: Set attr_accessible attributes on runtime.
|
48
|
+
email:
|
49
|
+
- dmitry.polushkin@gmail.com
|
131
50
|
executables: []
|
132
|
-
|
133
51
|
extensions: []
|
134
|
-
|
135
|
-
|
136
|
-
-
|
137
|
-
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .travis.yml
|
138
56
|
- Gemfile
|
139
57
|
- Gemfile.lock
|
140
58
|
- MIT-LICENSE
|
141
59
|
- README.md
|
142
60
|
- Rakefile
|
143
61
|
- attr_accessible_block.gemspec
|
144
|
-
- init.rb
|
145
|
-
- install.rb
|
146
62
|
- lib/attr_accessible_block.rb
|
147
|
-
-
|
148
|
-
-
|
149
|
-
-
|
150
|
-
|
151
|
-
|
152
|
-
has_rdoc: true
|
153
|
-
homepage: http://github.com/dmitry/attr_accessible_block
|
154
|
-
licenses:
|
155
|
-
- MIT
|
63
|
+
- lib/attr_accessible_block/version.rb
|
64
|
+
- spec/attr_accessible_block_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: https://github.com/dmitry/attr_accessible_block
|
67
|
+
licenses: []
|
156
68
|
post_install_message:
|
157
69
|
rdoc_options: []
|
158
|
-
|
159
|
-
require_paths:
|
70
|
+
require_paths:
|
160
71
|
- lib
|
161
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
73
|
none: false
|
163
|
-
requirements:
|
164
|
-
- -
|
165
|
-
- !ruby/object:Gem::Version
|
166
|
-
|
167
|
-
|
168
|
-
- 0
|
169
|
-
version: "0"
|
170
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
79
|
none: false
|
172
|
-
requirements:
|
173
|
-
- -
|
174
|
-
- !ruby/object:Gem::Version
|
175
|
-
|
176
|
-
segments:
|
177
|
-
- 0
|
178
|
-
version: "0"
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
179
84
|
requirements: []
|
180
|
-
|
181
85
|
rubyforge_project:
|
182
|
-
rubygems_version: 1.
|
86
|
+
rubygems_version: 1.8.10
|
183
87
|
signing_key:
|
184
88
|
specification_version: 3
|
185
|
-
summary:
|
186
|
-
|
187
|
-
|
188
|
-
- test/test_attr_accessible_block.rb
|
189
|
-
- test/test_helper.rb
|
89
|
+
summary: Bonus power attr_accessible on steroids with possibility to change accessibles
|
90
|
+
on the fly.
|
91
|
+
test_files: []
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'attr_accessible_block'
|
data/install.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# Install hook code here
|
data/test/schema.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
ActiveRecord::Schema.define(:version => 0) do
|
2
|
-
create_table :users, :force => true do |t|
|
3
|
-
t.string :email, :null => false
|
4
|
-
t.string :password, :null => false
|
5
|
-
t.string :role, :null => false
|
6
|
-
end
|
7
|
-
|
8
|
-
create_table :profiles, :force => true do |t|
|
9
|
-
t.references :user, :null => false
|
10
|
-
t.string :first_name, :null => false
|
11
|
-
t.string :last_name, :null => false
|
12
|
-
end
|
13
|
-
|
14
|
-
create_table :locations, :force => true do |t|
|
15
|
-
t.string :name, :null => false
|
16
|
-
t.string :code, :null => false
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class User < ActiveRecord::Base
|
21
|
-
attr_accessible do
|
22
|
-
add [:password, :profile_attributes]
|
23
|
-
add :email if record.new_record? || 'manager' == user.role
|
24
|
-
end
|
25
|
-
|
26
|
-
has_one :profile
|
27
|
-
accepts_nested_attributes_for :profile
|
28
|
-
|
29
|
-
before_create :set_default_role
|
30
|
-
|
31
|
-
validates_presence_of :email, :password
|
32
|
-
|
33
|
-
def self.current
|
34
|
-
User.last
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def set_default_role
|
40
|
-
self.role ||= 'default'
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
class Profile < ActiveRecord::Base
|
45
|
-
attr_accessible do
|
46
|
-
add [:first_name, :last_name] if record.new_record?
|
47
|
-
end
|
48
|
-
|
49
|
-
belongs_to :user
|
50
|
-
|
51
|
-
validates_presence_of :first_name, :last_name
|
52
|
-
end
|
53
|
-
|
54
|
-
class Location < ActiveRecord::Base
|
55
|
-
attr_accessible :name
|
56
|
-
|
57
|
-
validates_presence_of :name, :code
|
58
|
-
end
|
@@ -1,88 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
ActiveRecord::AttrAccessibleBlock.add_variable(:user) { User.current || User.new }
|
4
|
-
|
5
|
-
class AttrAccessibleBlockTest < Test::Unit::TestCase
|
6
|
-
def setup
|
7
|
-
setup_db
|
8
|
-
|
9
|
-
[User, Profile].each do |k|
|
10
|
-
k.delete_all
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def teardown
|
15
|
-
teardown_db
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_should_reject
|
19
|
-
user = User.create(:email => 'test@test.com', :password => 'test', :role => 'admin')
|
20
|
-
assert user.valid?
|
21
|
-
assert !user.new_record?
|
22
|
-
assert_equal 'test', user.password
|
23
|
-
assert_equal 'test@test.com', user.email
|
24
|
-
assert_equal 'default', user.role
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_should_always_accessible
|
28
|
-
assert_nil ActiveRecord::AttrAccessibleBlock.send(:class_variable_get, :@@always_accessible)
|
29
|
-
ActiveRecord::AttrAccessibleBlock.always_accessible { 'admin' == user.role }
|
30
|
-
assert_not_nil ActiveRecord::AttrAccessibleBlock.send(:class_variable_get, :@@always_accessible)
|
31
|
-
|
32
|
-
user = User.new(:email => 'test@test.com', :password => 'test', :profile_attributes => {:first_name => 'first name', :last_name => 'last name'})
|
33
|
-
user.role = 'admin'
|
34
|
-
assert user.save
|
35
|
-
assert !user.profile.new_record?
|
36
|
-
assert_equal 'admin', user.role
|
37
|
-
assert_equal 'first name', user.profile.first_name
|
38
|
-
assert user.update_attributes(:profile_attributes => {:id => user.profile.id, :first_name => 'first'})
|
39
|
-
assert_equal 'first', user.profile.first_name
|
40
|
-
|
41
|
-
ActiveRecord::AttrAccessibleBlock.send(:class_variable_set, :@@always_accessible, nil)
|
42
|
-
assert_nil ActiveRecord::AttrAccessibleBlock.send(:class_variable_get, :@@always_accessible)
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_should_change_only_on_create
|
46
|
-
user = User.create(:email => 'test@test.com', :password => 'test')
|
47
|
-
assert user.update_attributes(:email => 'new@new.com')
|
48
|
-
assert_equal 'test@test.com', user.email
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_should_work_attr_accessible_question_with_block
|
52
|
-
user = User.new(:email => 'test@test.com', :password => 'test')
|
53
|
-
assert_equal true, user.attr_accessible?(:email)
|
54
|
-
assert_equal true, user.attr_accessible?(:password)
|
55
|
-
user.save
|
56
|
-
assert_equal false, user.attr_accessible?(:email)
|
57
|
-
assert_equal true, user.attr_accessible?(:password)
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_should_work_attr_accessible_question_without_block
|
61
|
-
l = Location.new(:name => 'name', :code => 'code')
|
62
|
-
assert_equal true, l.attr_accessible?(:name)
|
63
|
-
assert_equal false, l.attr_accessible?(:code)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_should_access_to_add_variable_reader
|
67
|
-
user = User.new(:email => 'test@test.com', :password => 'test', :profile_attributes => {:first_name => 'first name', :last_name => 'last name'})
|
68
|
-
user.role = 'manager'
|
69
|
-
assert user.save
|
70
|
-
user.attributes = {:email => 'new@new.com'}
|
71
|
-
assert user.save
|
72
|
-
assert_equal 'new@new.com', user.email
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_simple_attr_accessible_should_work_as_expected
|
76
|
-
l = Location.create(:name => 'name', :code => 'code')
|
77
|
-
assert !l.valid?
|
78
|
-
assert_equal 'name', l.name
|
79
|
-
assert_equal nil, l.code
|
80
|
-
l.update_attribute(:code, 'code')
|
81
|
-
assert l.valid?
|
82
|
-
assert_equal 'code', l.code
|
83
|
-
l.code = 'changed'
|
84
|
-
l.save
|
85
|
-
assert l.valid?
|
86
|
-
assert_equal 'changed', l.code
|
87
|
-
end
|
88
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
gem 'activerecord', '~> 3.0'
|
5
|
-
|
6
|
-
require 'active_support'
|
7
|
-
require 'active_record'
|
8
|
-
require 'logger'
|
9
|
-
|
10
|
-
require 'attr_accessible_block'
|
11
|
-
|
12
|
-
ActiveRecord::Base.logger = Logger.new("test.log")
|
13
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
14
|
-
|
15
|
-
def setup_db
|
16
|
-
ActiveRecord::Migration.verbose = false
|
17
|
-
load "schema.rb"
|
18
|
-
end
|
19
|
-
|
20
|
-
def teardown_db
|
21
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
22
|
-
ActiveRecord::Base.connection.drop_table(table)
|
23
|
-
end
|
24
|
-
end
|
data/uninstall.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# Uninstall hook code here
|