hydra_attribute 0.3.2.beta → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -1
- data/features/attributes/create.feature +13 -5
- data/features/attributes/destroy.feature +14 -3
- data/features/attributes/update.feature +18 -1
- data/features/define.feature +9 -4
- data/features/step_definitions/model_steps.rb +9 -0
- data/features/step_definitions/record_steps.rb +4 -0
- data/features/support/env.rb +1 -15
- data/features/support/world.rb +16 -0
- data/lib/hydra_attribute/builder.rb +1 -3
- data/lib/hydra_attribute/hydra_attribute.rb +13 -8
- data/lib/hydra_attribute/version.rb +1 -1
- metadata +19 -19
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
+
**0.3.2 (July 31, 2012)**
|
2
|
+
* Add `white_list` option which allow to add attribute to white list for entity during creation it
|
3
|
+
|
1
4
|
**0.3.1 (July 28, 2012)**
|
2
5
|
* Fix bug "ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: name, backend_type, default_value" during creation hydra attributes
|
3
6
|
|
4
7
|
**0.3.0 (July 27, 2012)**
|
5
|
-
* All
|
8
|
+
* All attributes are now stored in database
|
6
9
|
* Support default value for attributes
|
7
10
|
* `#inspect` method displays hydra attributes too
|
8
11
|
|
@@ -7,7 +7,7 @@ Feature: create hydra attributes
|
|
7
7
|
| name | backend_type | white_list |
|
8
8
|
| [string:price] | [string:float] | [boolean:true] |
|
9
9
|
|
10
|
-
Scenario: create hydra attribute
|
10
|
+
Scenario: create hydra attribute
|
11
11
|
# Important: when respond_to? is called the hydra attributes are being loaded for entity class
|
12
12
|
Then model "Product" should respond to "price"
|
13
13
|
Given create hydra attributes for "Product" with role "admin" as "hashes":
|
@@ -15,8 +15,16 @@ Feature: create hydra attributes
|
|
15
15
|
| title | string |
|
16
16
|
Then model "Product" should respond to "title"
|
17
17
|
|
18
|
-
Scenario: create
|
18
|
+
Scenario: create attribute but don't add it to white list
|
19
19
|
Given create hydra attributes for "Product" with role "admin" as "hashes":
|
20
|
-
| name
|
21
|
-
| code
|
22
|
-
|
20
|
+
| name | backend_type | white_list |
|
21
|
+
| [string:code] | [string:string] | |
|
22
|
+
| [string:total] | [string:integer] | [boolean:false] |
|
23
|
+
Then class "Product" should not have "code" in white list
|
24
|
+
And class "Product" should not have "total" in white list
|
25
|
+
|
26
|
+
Scenario: create attribute and add it to white list
|
27
|
+
Given create hydra attributes for "Product" with role "admin" as "hashes":
|
28
|
+
| name | backend_type | white_list |
|
29
|
+
| [string:code] | [string:string] | [boolean:true] |
|
30
|
+
Then class "Product" should have "code" in white list
|
@@ -8,10 +8,21 @@ Feature: destroy hydra attributes
|
|
8
8
|
| name | backend_type | white_list |
|
9
9
|
| [string:price] | [string:float] | [boolean:true] |
|
10
10
|
|
11
|
-
|
11
|
+
|
12
|
+
Scenario: entity should not respond to removed attribute
|
13
|
+
When destroy all "HydraAttribute::HydraAttribute" models with attributes as "rows_hash":
|
14
|
+
|name | price |
|
15
|
+
Then model "Product" should not respond to "price"
|
16
|
+
|
17
|
+
|
18
|
+
Scenario: remove all values from appropriate table
|
12
19
|
Given create "Product" model with attributes as "rows_hash":
|
13
20
|
| price | 10 |
|
14
21
|
When destroy all "HydraAttribute::HydraAttribute" models with attributes as "rows_hash":
|
15
22
|
|name | price |
|
16
|
-
Then
|
17
|
-
|
23
|
+
Then total "HydraAttribute::HydraFloatProduct" records should be "0"
|
24
|
+
|
25
|
+
Scenario: remove attribute from white list
|
26
|
+
When destroy all "HydraAttribute::HydraAttribute" models with attributes as "rows_hash":
|
27
|
+
|name | price |
|
28
|
+
Then class "Product" should not have "price" in white list
|
@@ -7,7 +7,7 @@ Feature: update hydra attribute
|
|
7
7
|
| name | backend_type | default_value | white_list |
|
8
8
|
| [string:code] | [string:integer] | [integer:1] | [boolean:true] |
|
9
9
|
|
10
|
-
Scenario: update default value
|
10
|
+
Scenario: update default value
|
11
11
|
Given create "Product" model
|
12
12
|
And load and update attributes for "HydraAttribute::HydraAttribute" models with attributes as "rows_hash":
|
13
13
|
| default_value | 2 |
|
@@ -17,3 +17,20 @@ Feature: update hydra attribute
|
|
17
17
|
And last created "Product" should have the following attributes:
|
18
18
|
| code | [integer:2] |
|
19
19
|
|
20
|
+
Scenario: update white list attribute to true
|
21
|
+
Given create hydra attributes for "Product" with role "admin" as "hashes":
|
22
|
+
| name | backend_type | white_list |
|
23
|
+
| [string:title] | [string:string] | [boolean:false] |
|
24
|
+
And select last "HydraAttribute::HydraAttribute" record
|
25
|
+
When update attributes as "admin":
|
26
|
+
| white_list | [boolean:true] |
|
27
|
+
Then class "Product" should have "title" in white list
|
28
|
+
|
29
|
+
Scenario: update white list attribute to false
|
30
|
+
Given create hydra attributes for "Product" with role "admin" as "hashes":
|
31
|
+
| name | backend_type | white_list |
|
32
|
+
| [string:info] | [string:string] | [boolean:true] |
|
33
|
+
And select last "HydraAttribute::HydraAttribute" record
|
34
|
+
When update attributes as "admin":
|
35
|
+
| white_list | [boolean:false] |
|
36
|
+
Then class "Product" should not have "info" in white list
|
data/features/define.feature
CHANGED
@@ -4,9 +4,9 @@ Feature: define hydra attributes
|
|
4
4
|
|
5
5
|
Background: create hydra attributes
|
6
6
|
Given create hydra attributes for "Product" with role "admin" as "hashes":
|
7
|
-
| name | backend_type |
|
8
|
-
| [string:code] | [string:string] |
|
9
|
-
| [string:price] | [string:float] |
|
7
|
+
| name | backend_type | white_list |
|
8
|
+
| [string:code] | [string:string] | [boolean:true] |
|
9
|
+
| [string:price] | [string:float] | [boolean:false] |
|
10
10
|
|
11
11
|
Scenario Outline: models should respond to hydra attributes
|
12
12
|
Then model "<model>" <action> respond to "<attributes>"
|
@@ -30,4 +30,9 @@ Feature: define hydra attributes
|
|
30
30
|
| Product | should | price_change |
|
31
31
|
| Product | should | price_will_change! |
|
32
32
|
| Product | should | price_was |
|
33
|
-
| Product | should | reset_price! |
|
33
|
+
| Product | should | reset_price! |
|
34
|
+
|
35
|
+
Scenario: model should have appropriate attributes in white list
|
36
|
+
When redefine "Product" class to use hydra attributes
|
37
|
+
Then class "Product" should have "code" in white list
|
38
|
+
And class "Product" should not have "price" in white list
|
@@ -1,3 +1,7 @@
|
|
1
|
+
Given /^redefine "([^"]+)" class to use hydra attributes$/ do |klass|
|
2
|
+
redefine_hydra_entity(klass)
|
3
|
+
end
|
4
|
+
|
1
5
|
Given /^create "([^"]+)" model$/ do |klass|
|
2
6
|
klass.constantize.create!
|
3
7
|
end
|
@@ -38,4 +42,9 @@ Then /^(last|first) created "([^"]+)" (should|should not) have the following att
|
|
38
42
|
table.rows_hash.each_with_object(klass.constantize.send(method)) do |(attribute, value), model|
|
39
43
|
model.send(attribute).send(match) == type_cast_value(value)
|
40
44
|
end
|
45
|
+
end
|
46
|
+
|
47
|
+
Then /^class "([^"]+)" (should(?:\snot)?) have "([^"]+)" in white list$/ do |klass, accept, attribute|
|
48
|
+
method = accept.sub(' ', '_')
|
49
|
+
klass.constantize.accessible_attributes.send(method, include(attribute))
|
41
50
|
end
|
@@ -21,6 +21,10 @@ When /^keep "([^"]+)" attribute$/ do |attribute|
|
|
21
21
|
@keep[attribute] = @record.send(attribute)
|
22
22
|
end
|
23
23
|
|
24
|
+
Then /^update attributes as "([^"]+)":$/ do |role, table|
|
25
|
+
@record.update_attributes(type_cast_hash(table.rows_hash), as: role.to_sym)
|
26
|
+
end
|
27
|
+
|
24
28
|
Then /^record should be nil$/ do
|
25
29
|
@record.should be_nil
|
26
30
|
end
|
data/features/support/env.rb
CHANGED
@@ -4,7 +4,6 @@ require 'database_cleaner'
|
|
4
4
|
require 'database_cleaner/cucumber'
|
5
5
|
|
6
6
|
ActiveSupport.on_load(:active_record) do
|
7
|
-
self.attr_accessible :name # we create entity model with one column "name" for testing
|
8
7
|
self.default_timezone = :utc
|
9
8
|
unless ActiveRecord::VERSION::STRING.start_with?('3.1.')
|
10
9
|
self.mass_assignment_sanitizer = :strict
|
@@ -16,21 +15,8 @@ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:'
|
|
16
15
|
DatabaseCleaner.strategy = :truncation
|
17
16
|
|
18
17
|
Before do
|
19
|
-
|
20
|
-
HydraAttribute::SUPPORT_TYPES.each do |type|
|
21
|
-
assoc_model = HydraAttribute::AssociationBuilder.new(Product, type).model_name
|
22
|
-
HydraAttribute.send(:remove_const, assoc_model)
|
23
|
-
end
|
24
|
-
|
25
|
-
Object.send(:remove_const, :Product)
|
26
|
-
end
|
27
|
-
|
28
|
-
ActiveSupport::Dependencies::Reference.clear!
|
18
|
+
redefine_hydra_entity('Product')
|
29
19
|
DatabaseCleaner.start
|
30
|
-
|
31
|
-
class Product < ActiveRecord::Base
|
32
|
-
use_hydra_attributes
|
33
|
-
end
|
34
20
|
end
|
35
21
|
|
36
22
|
After do
|
data/features/support/world.rb
CHANGED
@@ -33,6 +33,22 @@ module HydraAttribute
|
|
33
33
|
hash[key] = type_cast_value(value)
|
34
34
|
end
|
35
35
|
end
|
36
|
+
|
37
|
+
def redefine_hydra_entity(klass)
|
38
|
+
::ActiveSupport::Dependencies.clear
|
39
|
+
|
40
|
+
Object.send(:remove_const, klass.to_sym) if Object.const_defined?(klass.to_sym)
|
41
|
+
|
42
|
+
::HydraAttribute::SUPPORT_TYPES.each do |type|
|
43
|
+
class_name = "Hydra#{type.capitalize}#{klass}".to_sym
|
44
|
+
::HydraAttribute.send(:remove_const, class_name) if ::HydraAttribute.const_defined?(class_name)
|
45
|
+
end
|
46
|
+
|
47
|
+
Object.const_set(klass.to_sym, Class.new(::ActiveRecord::Base))
|
48
|
+
klass.to_s.constantize.send(:accessible_attributes_configs).values.each(&:clear)
|
49
|
+
klass.to_s.constantize.attr_accessible :name
|
50
|
+
klass.to_s.constantize.use_hydra_attributes
|
51
|
+
end
|
36
52
|
end
|
37
53
|
end
|
38
54
|
end
|
@@ -32,9 +32,7 @@ module HydraAttribute
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def build_white_list
|
35
|
-
klass.hydra_attributes.each
|
36
|
-
klass.accessible_attributes.add(hydra_attribute.name) if hydra_attribute.white_list?
|
37
|
-
end
|
35
|
+
klass.hydra_attributes.each(&:toggle_white_list!)
|
38
36
|
end
|
39
37
|
end
|
40
38
|
end
|
@@ -16,7 +16,16 @@ module HydraAttribute
|
|
16
16
|
end
|
17
17
|
|
18
18
|
before_destroy :delete_dependent_values
|
19
|
-
after_commit :
|
19
|
+
after_commit :reload_attribute_methods
|
20
|
+
after_commit :toggle_white_list!
|
21
|
+
|
22
|
+
def toggle_white_list!
|
23
|
+
if destroyed? or !white_list?
|
24
|
+
remove_from_white_list
|
25
|
+
else
|
26
|
+
add_to_white_list
|
27
|
+
end
|
28
|
+
end
|
20
29
|
|
21
30
|
private
|
22
31
|
|
@@ -25,20 +34,16 @@ module HydraAttribute
|
|
25
34
|
value_class.delete_all(hydra_attribute_id: id)
|
26
35
|
end
|
27
36
|
|
28
|
-
def
|
37
|
+
def reload_attribute_methods
|
29
38
|
entity_type.constantize.reset_hydra_attribute_methods # TODO should not remove all generated methods just for this attribute
|
30
|
-
destroyed? ? remove_from_white_list : add_to_white_list
|
31
39
|
end
|
32
40
|
|
33
|
-
# Add attribute to white list for entity if it has a white list mark
|
34
41
|
def add_to_white_list
|
35
|
-
entity_type.constantize.accessible_attributes.add(name)
|
42
|
+
entity_type.constantize.accessible_attributes.add(name)
|
36
43
|
end
|
37
44
|
|
38
|
-
# Don't check if this attribute is in white list or has a white list mark.
|
39
|
-
# Just remove it from white list for entity
|
40
45
|
def remove_from_white_list
|
41
|
-
entity_type.constantize.accessible_attributes.
|
46
|
+
entity_type.constantize.accessible_attributes.delete(name)
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
metadata
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hydra_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.2
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.2
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kostyantyn Stepanyuk
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-31 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152407120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.1.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152407120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152406720 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152406720
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: cucumber
|
38
|
-
requirement: &
|
38
|
+
requirement: &2152406180 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2152406180
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: sqlite3
|
49
|
-
requirement: &
|
49
|
+
requirement: &2152405740 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2152405740
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: database_cleaner
|
60
|
-
requirement: &
|
60
|
+
requirement: &2152405300 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2152405300
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: appraisal
|
71
|
-
requirement: &
|
71
|
+
requirement: &2152404880 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *2152404880
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rake
|
82
|
-
requirement: &
|
82
|
+
requirement: &2152404460 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *2152404460
|
91
91
|
description: hydra_attribute is an implementation of EAV pattern for ActiveRecord
|
92
92
|
models.
|
93
93
|
email: kostya.stepanyuk@gmail.com
|
@@ -164,9 +164,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
164
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
165
|
none: false
|
166
166
|
requirements:
|
167
|
-
- - ! '
|
167
|
+
- - ! '>='
|
168
168
|
- !ruby/object:Gem::Version
|
169
|
-
version:
|
169
|
+
version: '0'
|
170
170
|
requirements: []
|
171
171
|
rubyforge_project:
|
172
172
|
rubygems_version: 1.8.10
|