hydra_attribute 0.5.0.rc1 → 0.5.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -1
- data/lib/hydra_attribute/active_record/attribute_methods.rb +13 -0
- data/lib/hydra_attribute/active_record/mass_assignment_security.rb +5 -1
- data/lib/hydra_attribute/hydra_value.rb +1 -1
- data/lib/hydra_attribute/model.rb +7 -0
- data/lib/hydra_attribute/model/persistence.rb +1 -1
- data/lib/hydra_attribute/railtie.rb +5 -0
- data/lib/hydra_attribute/version.rb +1 -1
- data/spec/hydra_attribute/active_record/attribute_methods_spec.rb +21 -0
- data/spec/hydra_attribute/active_record/mass_assignment_security_spec.rb +34 -6
- metadata +2 -2
data/.gitignore
CHANGED
@@ -42,6 +42,19 @@ module HydraAttribute
|
|
42
42
|
end
|
43
43
|
super
|
44
44
|
end
|
45
|
+
|
46
|
+
# Returns the column object for the named attribute.
|
47
|
+
#
|
48
|
+
# @param [String, Symbol] name
|
49
|
+
# @return [ActiveRecord::ConnectionAdapters::Column]
|
50
|
+
def column_for_attribute(name)
|
51
|
+
hydra_attribute = self.class.hydra_attributes.find { |hydra_attribute| hydra_attribute.name == name.to_s } # TODO should be cached
|
52
|
+
if hydra_attribute
|
53
|
+
HydraValue.column(hydra_attribute.id)
|
54
|
+
else
|
55
|
+
super
|
56
|
+
end
|
57
|
+
end
|
45
58
|
end
|
46
59
|
end
|
47
60
|
end
|
@@ -25,9 +25,13 @@ module HydraAttribute
|
|
25
25
|
# TODO should be optimized. List of allowed attributes should be cached
|
26
26
|
def hydra_attribute_by_name(attribute_name)
|
27
27
|
::HydraAttribute::HydraAttribute.all_by_entity_type(@entity.class.name).find do |attribute|
|
28
|
-
attribute.name == attribute_name
|
28
|
+
attribute.name == remove_multiparameter_id(attribute_name)
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
def remove_multiparameter_id(attribute_name)
|
33
|
+
attribute_name.to_s.gsub(/\(.+/m, '')
|
34
|
+
end
|
31
35
|
end
|
32
36
|
|
33
37
|
protected
|
@@ -21,6 +21,13 @@ module HydraAttribute
|
|
21
21
|
include Dirty
|
22
22
|
include HasManyThrough
|
23
23
|
|
24
|
+
included do
|
25
|
+
# Compatibility with Rails' helpers
|
26
|
+
include ActiveModel::Conversion
|
27
|
+
extend ActiveModel::Naming
|
28
|
+
extend ActiveModel::Translation
|
29
|
+
end
|
30
|
+
|
24
31
|
module ClassMethods
|
25
32
|
# Find first model
|
26
33
|
#
|
@@ -5,5 +5,10 @@ module HydraAttribute
|
|
5
5
|
::ActiveRecord::Migration.send(:include, ::HydraAttribute::ActiveRecord::Migration)
|
6
6
|
end
|
7
7
|
end
|
8
|
+
|
9
|
+
initializer 'hydra_attribute.middleware' do |app|
|
10
|
+
require 'hydra_attribute/middleware/identity_map'
|
11
|
+
app.middleware.use ::HydraAttribute::Middleware::IdentityMap
|
12
|
+
end
|
8
13
|
end
|
9
14
|
end
|
@@ -31,4 +31,25 @@ describe HydraAttribute::ActiveRecord::AttributeMethods do
|
|
31
31
|
product.read_attribute(:name).should be_nil
|
32
32
|
end
|
33
33
|
end
|
34
|
+
|
35
|
+
describe '#column_for_attribute' do
|
36
|
+
let!(:hydra_attribute) { Product.hydra_attributes.create(name: 'code', backend_type: 'string', default_value: 'abc') }
|
37
|
+
let(:product) { Product.new }
|
38
|
+
|
39
|
+
it 'should return column for hydra attribute' do
|
40
|
+
column = product.column_for_attribute(:code)
|
41
|
+
column.should be_kind_of(ActiveRecord::ConnectionAdapters::Column)
|
42
|
+
column.name.should == 'code'
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should return column for static attribute' do
|
46
|
+
column = product.column_for_attribute(:name)
|
47
|
+
column.should be_kind_of(ActiveRecord::ConnectionAdapters::Column)
|
48
|
+
column.name.should == 'name'
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should return nil if cannot find attribute' do
|
52
|
+
product.column_for_attribute(:fake).should be_nil
|
53
|
+
end
|
54
|
+
end
|
34
55
|
end
|
@@ -3,14 +3,28 @@ require 'spec_helper'
|
|
3
3
|
module HydraAttribute::ActiveRecord::MassAssignmentSecurity
|
4
4
|
describe 'WhiteList' do
|
5
5
|
before do
|
6
|
-
ProductWhiteList.hydra_attributes.create(name: 'color', backend_type: 'string',
|
7
|
-
ProductWhiteList.hydra_attributes.create(name: 'code', backend_type: 'string',
|
6
|
+
ProductWhiteList.hydra_attributes.create(name: 'color', backend_type: 'string', white_list: true)
|
7
|
+
ProductWhiteList.hydra_attributes.create(name: 'code', backend_type: 'string', white_list: false)
|
8
|
+
ProductWhiteList.hydra_attributes.create(name: 'seen', backend_type: 'datetime', white_list: true)
|
8
9
|
end
|
9
10
|
|
10
|
-
let(:product)
|
11
|
+
let(:product) do
|
12
|
+
ProductWhiteList.new(
|
13
|
+
:name => 'name',
|
14
|
+
:title => 'title',
|
15
|
+
:color => 'green',
|
16
|
+
:code => 'abc',
|
17
|
+
'seen(1i)' => '2012',
|
18
|
+
'seen(2i)' => '12',
|
19
|
+
'seen(3i)' => '22',
|
20
|
+
'seen(4i)' => '15',
|
21
|
+
'seen(5i)' => '30'
|
22
|
+
)
|
23
|
+
end
|
11
24
|
|
12
25
|
it 'should assign only hydra attributes which are in white list' do
|
13
26
|
product.color.should == 'green'
|
27
|
+
product.seen.should == DateTime.new(2012, 12, 22, 15, 30)
|
14
28
|
product.code.should be_nil
|
15
29
|
end
|
16
30
|
|
@@ -22,14 +36,28 @@ module HydraAttribute::ActiveRecord::MassAssignmentSecurity
|
|
22
36
|
|
23
37
|
describe 'BlackList' do
|
24
38
|
before do
|
25
|
-
ProductBlackList.hydra_attributes.create(name: 'color', backend_type: 'string',
|
26
|
-
ProductBlackList.hydra_attributes.create(name: 'code', backend_type: 'string',
|
39
|
+
ProductBlackList.hydra_attributes.create(name: 'color', backend_type: 'string', white_list: true)
|
40
|
+
ProductBlackList.hydra_attributes.create(name: 'code', backend_type: 'string', white_list: false)
|
41
|
+
ProductBlackList.hydra_attributes.create(name: 'seen', backend_type: 'datetime', white_list: true)
|
27
42
|
end
|
28
43
|
|
29
|
-
let(:product)
|
44
|
+
let(:product) do
|
45
|
+
ProductBlackList.new(
|
46
|
+
:name => 'name',
|
47
|
+
:title => 'title',
|
48
|
+
:color => 'green',
|
49
|
+
:code => 'abc',
|
50
|
+
'seen(1i)' => '2012',
|
51
|
+
'seen(2i)' => '12',
|
52
|
+
'seen(3i)' => '22',
|
53
|
+
'seen(4i)' => '15',
|
54
|
+
'seen(5i)' => '30'
|
55
|
+
)
|
56
|
+
end
|
30
57
|
|
31
58
|
it 'should assign only hydra attributes which are in white list' do
|
32
59
|
product.color.should == 'green'
|
60
|
+
product.seen.should == DateTime.new(2012, 12, 22, 15, 30)
|
33
61
|
product.code.should be_nil
|
34
62
|
end
|
35
63
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hydra_attribute
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.0.
|
4
|
+
version: 0.5.0.rc2
|
5
5
|
prerelease: 6
|
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:
|
12
|
+
date: 2014-01-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|