hydra_attribute 0.4.2 → 0.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/.gitignore +2 -1
  2. data/.travis.yml +6 -5
  3. data/CHANGELOG.md +6 -0
  4. data/Gemfile +1 -1
  5. data/README.md +3 -3
  6. data/Rakefile +2 -7
  7. data/gemfiles/activerecord-3.2.gemfile +5 -0
  8. data/hydra_attribute.gemspec +6 -7
  9. data/lib/hydra_attribute.rb +17 -18
  10. data/lib/hydra_attribute/active_record.rb +34 -13
  11. data/lib/hydra_attribute/active_record/association_preloader.rb +47 -28
  12. data/lib/hydra_attribute/active_record/attribute_methods.rb +29 -140
  13. data/lib/hydra_attribute/active_record/mass_assignment_security.rb +39 -0
  14. data/lib/hydra_attribute/active_record/migration.rb +4 -4
  15. data/lib/hydra_attribute/active_record/relation.rb +6 -7
  16. data/lib/hydra_attribute/active_record/relation/query_methods.rb +28 -18
  17. data/lib/hydra_attribute/hydra_attribute.rb +12 -49
  18. data/lib/hydra_attribute/hydra_attribute_set.rb +67 -0
  19. data/lib/hydra_attribute/hydra_entity.rb +110 -0
  20. data/lib/hydra_attribute/hydra_entity_attribute_association.rb +155 -0
  21. data/lib/hydra_attribute/hydra_set.rb +24 -26
  22. data/lib/hydra_attribute/hydra_value.rb +210 -0
  23. data/lib/hydra_attribute/identity_map.rb +18 -0
  24. data/lib/hydra_attribute/middleware/identity_map.rb +15 -0
  25. data/lib/hydra_attribute/migrator.rb +24 -21
  26. data/lib/hydra_attribute/model.rb +47 -0
  27. data/lib/hydra_attribute/model/cacheable.rb +207 -0
  28. data/lib/hydra_attribute/model/dirty.rb +39 -0
  29. data/lib/hydra_attribute/model/has_many_through.rb +168 -0
  30. data/lib/hydra_attribute/model/identity_map.rb +59 -0
  31. data/lib/hydra_attribute/model/mediator.rb +89 -0
  32. data/lib/hydra_attribute/model/notifiable.rb +23 -0
  33. data/lib/hydra_attribute/model/persistence.rb +424 -0
  34. data/lib/hydra_attribute/model/validations.rb +40 -0
  35. data/lib/hydra_attribute/version.rb +1 -1
  36. data/spec/environments/mysql.rb +23 -0
  37. data/spec/environments/postgresql.rb +23 -0
  38. data/spec/environments/sqlite.rb +12 -0
  39. data/spec/fixtures/category.rb +8 -0
  40. data/spec/fixtures/product.rb +8 -0
  41. data/spec/fixtures/product_black_list.rb +13 -0
  42. data/spec/fixtures/product_white_list.rb +13 -0
  43. data/spec/hydra_attribute/active_record/attribute_methods_spec.rb +23 -28
  44. data/spec/hydra_attribute/active_record/mass_assignment_security_spec.rb +41 -0
  45. data/spec/hydra_attribute/active_record_spec.rb +577 -0
  46. data/spec/hydra_attribute/hydra_attribute_set_spec.rb +651 -0
  47. data/spec/hydra_attribute/hydra_attribute_spec.rb +208 -10
  48. data/spec/hydra_attribute/hydra_entity_attribute_association_spec.rb +216 -0
  49. data/spec/hydra_attribute/hydra_entity_spec.rb +71 -0
  50. data/spec/hydra_attribute/hydra_set_spec.rb +51 -10
  51. data/spec/hydra_attribute/hydra_value_spec.rb +286 -0
  52. data/spec/hydra_attribute/identity_map_spec.rb +47 -0
  53. data/spec/hydra_attribute/migrator_spec.rb +411 -0
  54. data/spec/hydra_attribute/model/cacheable_spec.rb +106 -0
  55. data/spec/hydra_attribute/model/has_many_through_spec.rb +132 -0
  56. data/spec/hydra_attribute/model/identity_map_spec.rb +39 -0
  57. data/spec/hydra_attribute/model/mediator_spec.rb +62 -0
  58. data/spec/hydra_attribute/model/persistence_spec.rb +550 -0
  59. data/spec/hydra_attribute/model_spec.rb +39 -0
  60. data/spec/hydra_attribute_spec.rb +36 -0
  61. data/spec/spec_helper.rb +10 -42
  62. metadata +76 -100
  63. data/Appraisals +0 -7
  64. data/cucumber.yml +0 -1
  65. data/features/entity/create.feature +0 -145
  66. data/features/entity/destroy.feature +0 -111
  67. data/features/entity/new.feature +0 -121
  68. data/features/entity/update.feature +0 -147
  69. data/features/hydra_attribute/create.feature +0 -30
  70. data/features/hydra_attribute/destroy.feature +0 -26
  71. data/features/hydra_attribute/update.feature +0 -36
  72. data/features/hydra_set/destroy.feature +0 -31
  73. data/features/migrations/create_and_drop.feature +0 -165
  74. data/features/migrations/migrate_and_rollback.feature +0 -211
  75. data/features/relation/query_methods/group.feature +0 -42
  76. data/features/relation/query_methods/order.feature +0 -67
  77. data/features/relation/query_methods/reorder.feature +0 -29
  78. data/features/relation/query_methods/reverse_order.feature +0 -29
  79. data/features/relation/query_methods/select.feature +0 -50
  80. data/features/relation/query_methods/where.feature +0 -115
  81. data/features/step_definitions/connections.rb +0 -65
  82. data/features/step_definitions/model_steps.rb +0 -136
  83. data/features/step_definitions/query_methods.rb +0 -48
  84. data/features/step_definitions/record_steps.rb +0 -93
  85. data/features/support/env.rb +0 -38
  86. data/features/support/world.rb +0 -61
  87. data/lib/hydra_attribute/active_record/association.rb +0 -113
  88. data/lib/hydra_attribute/active_record/reflection.rb +0 -16
  89. data/lib/hydra_attribute/association_builder.rb +0 -69
  90. data/lib/hydra_attribute/builder.rb +0 -37
  91. data/lib/hydra_attribute/entity_callbacks.rb +0 -26
  92. data/lib/hydra_attribute/hydra_attribute_methods.rb +0 -226
  93. data/lib/hydra_attribute/hydra_methods.rb +0 -528
  94. data/lib/hydra_attribute/hydra_set_methods.rb +0 -95
  95. data/lib/hydra_attribute/hydra_value_methods.rb +0 -21
  96. data/lib/hydra_attribute/memoizable.rb +0 -37
  97. data/spec/hydra_attribute/active_record/relation/query_methods_spec.rb +0 -31
  98. data/spec/hydra_attribute/hydra_attribute_methods_spec.rb +0 -458
  99. data/spec/hydra_attribute/hydra_methods_spec.rb +0 -456
  100. data/spec/hydra_attribute/hydra_set_methods_spec.rb +0 -203
  101. data/spec/hydra_attribute/memoizable_spec.rb +0 -95
@@ -1,48 +0,0 @@
1
- When /^filter "([^"]+)" by:$/ do |klass, table|
2
- condition = table.hashes.each_with_object({}) { |item, hash| hash[item[:field].to_sym] = type_cast_value(item[:value]) }
3
- @records = Object.const_get(klass).where(condition)
4
- end
5
-
6
- When /^filter "([^"]+)" records by "([^"]+)"$/ do |klass, attribute|
7
- @records = Object.const_get(klass)
8
- step %(filter records by "#{attribute}")
9
- end
10
-
11
- When /^filter records by "([^"]+)"$/ do |attribute|
12
- name, value = type_cast_attribute(attribute)
13
- @records = @records.where(name => value)
14
- end
15
-
16
- When /^group "([^"]+)" by "([^"]+)"$/ do |klass, attributes|
17
- @records = Object.const_get(klass)
18
- step %(group by "#{attributes}")
19
- end
20
-
21
- When /^group by "([^"]+)"$/ do |attributes|
22
- @records = @records.group(attributes.split)
23
- end
24
-
25
- When /^(order|reorder) "([^"]+)" records by "([^"]+)"$/ do |sort_method, klass, attributes|
26
- @records = Object.const_get(klass)
27
- step %(#{sort_method} records by "#{attributes}")
28
- end
29
-
30
- When /^(order|reorder) records by "([^"]+)"$/ do |sort_method, attributes|
31
- reverse = false
32
- fields = attributes.split.inject([]) do |items, attribute|
33
- name, direction = attribute.split('=')
34
- reverse = true if direction == 'desc'
35
- items << name.to_sym
36
- end
37
-
38
- @records = @records.send(sort_method, fields)
39
- @records = @records.reverse_order if reverse
40
- end
41
-
42
- When /^reverse order records$/ do
43
- @records = @records.reverse_order
44
- end
45
-
46
- When /^"([^"]+)" select only the following columns "([^"]+)"$/ do |klass, columns|
47
- @records = Object.const_get(klass).select(columns.split(/\s+/).map(&:to_sym))
48
- end
@@ -1,93 +0,0 @@
1
- When /^load all "([^"]+)" records$/ do |klass|
2
- @records = klass.constantize.all
3
- end
4
-
5
- When /^select (first|last) "([^"]+)" record$/ do |method, klass|
6
- @record = klass.constantize.send(method)
7
- end
8
-
9
- When /^assign attributes as "([^"]+)":$/ do |format, table|
10
- Array.wrap(table.send(format)).each do |hash|
11
- @record.assign_attributes(type_cast_hash(hash))
12
- end
13
- end
14
-
15
- When /^(save|destroy) record$/ do |action|
16
- @record.send(action)
17
- end
18
-
19
- When /^keep "([^"]+)" attribute$/ do |attribute|
20
- @keep ||= {}
21
- @keep[attribute] = @record.send(attribute)
22
- end
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
-
28
- Then /^record should be nil$/ do
29
- @record.should be_nil
30
- end
31
-
32
- Then /^attribute "([^"]+)" (should(?:\snot)?) be the same$/ do |attribute, behavior|
33
- method = behavior.sub(/\s/, '_')
34
- @keep[attribute].send(method) == @record.send(attribute)
35
- end
36
-
37
- Then /^record (read attribute(?: before type cast)?) "([^"]+)" and value should be "([^"]+)"$/ do |method, attribute, value|
38
- method = method.gsub(/\s+/, '_')
39
- @record.send(method, attribute).should == type_cast_value(value)
40
- end
41
-
42
- Then /^"(first|last)" record should have "([^"]+)"$/ do |method, attribute|
43
- type_cast_attributes(attribute).each do |(name, value)|
44
- @records.send(method).send(name).should == value
45
- end
46
- end
47
-
48
- Then /^records should have the following attributes:$/ do |table|
49
- table.hashes.each do |hash|
50
- record = @records.detect { |r| r.send(hash[:field]) == type_cast_value(hash[:value]) }
51
- record.should_not be_nil
52
- end
53
- end
54
-
55
- Then /^records should have only the following "([^"]+)" names$/ do |attributes|
56
- @records.each do |record|
57
- record.attributes.keys.should =~ attributes.split(/\s+/)
58
- end
59
- end
60
-
61
- Then /^records should raise "([^"]+)" when call the following "([^"]+)"$/ do |error_class, methods|
62
- @records.each do |record|
63
- methods.split(/\s+/).each do |method|
64
- lambda { record.send(method) }.should raise_error(error_class.constantize)
65
- end
66
- end
67
- end
68
-
69
- Then /^total records should be "([^"]+)"$/ do |count|
70
- @records.to_a.should have(count.to_i).records
71
- end
72
-
73
- Then /^total "([^"]+)" records should be "([^"]+)"$/ do |klass, count|
74
- @records = klass.constantize.scoped
75
- step %(total records should be "#{count}")
76
- end
77
-
78
- Then /^records "(should|should_not)" have loaded associations:$/ do |should, table|
79
- table.hashes.each do |hash|
80
- @records.each do |record|
81
- record.association(hash[:association].to_sym).send(should, be_loaded)
82
- end
83
- end
84
- end
85
-
86
- Then /^"([^"]+)" records "(should|should_not)" have loaded associations:$/ do |klass, should, table|
87
- table.hashes.each do |hash|
88
- records = @records.select { |record| record.class.name == klass }
89
- records.each do |record|
90
- record.association(hash[:association].to_sym).send(should, be_loaded)
91
- end
92
- end
93
- end
@@ -1,38 +0,0 @@
1
- require 'active_record'
2
- require 'hydra_attribute'
3
- require 'database_cleaner'
4
-
5
- ActiveSupport.on_load(:active_record) do
6
- self.default_timezone = :utc
7
- unless ActiveRecord::VERSION::STRING.start_with?('3.1.') # @COMPATIBILITY with 3.1.x. active_record 3.1 doesn't have "mass_assignment_sanitizer" method
8
- self.mass_assignment_sanitizer = :strict
9
- end
10
-
11
- ActiveRecord::Migration.send(:include, HydraAttribute::ActiveRecord::Migration)
12
- end
13
-
14
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
15
- DatabaseCleaner.strategy = :truncation
16
-
17
- class Migration < ActiveRecord::Migration
18
- def up
19
- create_hydra_entity :products do |t|
20
- t.string :name
21
- t.timestamps
22
- end
23
- end
24
-
25
- def down
26
- end
27
- end
28
-
29
- Migration.new.up
30
-
31
- Before do
32
- redefine_hydra_entity('Product')
33
- DatabaseCleaner.start
34
- end
35
-
36
- After do
37
- DatabaseCleaner.clean
38
- end
@@ -1,61 +0,0 @@
1
- module HydraAttribute
2
- module Cucumber
3
- module World
4
- def type_cast_value(schema)
5
- return schema unless schema.is_a?(String)
6
- type, value = schema.gsub(/(^\[)|(\]$)/, '').split(':', 2)
7
- return schema if schema == type && value.nil?
8
-
9
- case type
10
- when 'int' then type_cast_value(value).to_i
11
- when 'float' then type_cast_value(value).to_f
12
- when 'bool' then ::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(type_cast_value(value))
13
- when 'nil' then nil
14
- when 'date' then ActiveSupport::TimeZone.new('UTC').parse(type_cast_value(value))
15
- when 'sym' then type_cast_value(value).to_sym
16
- when 'array' then value.split(',').map { |v| type_cast_value(v) }
17
- when 'eval' then eval(type_cast_value(value))
18
- when 'str' then type_cast_value(value).to_s
19
- else value
20
- end
21
- end
22
-
23
- def type_cast_attribute(attribute)
24
- name, schema = attribute.split('=')
25
- [name, type_cast_value(schema)]
26
- end
27
-
28
- def type_cast_attributes(attributes)
29
- # 'a=[b c] c=a c=[v abc]' => ["a=[b c]", "c=a", "c=[v abc]"]
30
- attributes.gsub(/\s+(\w+=)/, '<###>\1').split('<###>').each_with_object({}) do |attribute, hash|
31
- name, value = type_cast_attribute(attribute)
32
- hash[name] = value
33
- end
34
- end
35
-
36
- def type_cast_hash(hash)
37
- hash.delete_if{ |_, v| v.blank? }.each do |key, value|
38
- hash[key] = type_cast_value(value)
39
- end
40
- end
41
-
42
- def redefine_hydra_entity(klass)
43
- ::ActiveSupport::Dependencies.clear
44
-
45
- Object.send(:remove_const, klass.to_sym) if Object.const_defined?(klass.to_sym)
46
-
47
- ::HydraAttribute::SUPPORTED_BACKEND_TYPES.each do |type|
48
- class_name = "Hydra#{type.capitalize}#{klass}".to_sym
49
- ::HydraAttribute.send(:remove_const, class_name) if ::HydraAttribute.const_defined?(class_name)
50
- end
51
-
52
- Object.const_set(klass.to_sym, Class.new(::ActiveRecord::Base))
53
- klass.to_s.constantize.send(:accessible_attributes_configs).values.each(&:clear)
54
- klass.to_s.constantize.attr_accessible :name, :hydra_set_id
55
- klass.to_s.constantize.send(:include, ::HydraAttribute::ActiveRecord)
56
- end
57
- end
58
- end
59
- end
60
-
61
- World(HydraAttribute::Cucumber::World)
@@ -1,113 +0,0 @@
1
- module HydraAttribute
2
- module ActiveRecord
3
- class Association < ::ActiveRecord::Associations::HasManyAssociation
4
-
5
- def find_model(hydra_attribute_id)
6
- load_target unless loaded?
7
-
8
- hydra_set_target.detect do |model|
9
- model.hydra_attribute_id == hydra_attribute_id
10
- end
11
- end
12
-
13
- def find_model_or_build(options = {})
14
- find_model(options[:hydra_attribute_id]) || build(options)
15
- end
16
-
17
- def build(attributes = {}, options = {}, &block)
18
- return if hydra_attribute_ids.exclude?(attributes[:hydra_attribute_id])
19
- return if target.any? { |model| model.hydra_attribute_id == attributes[:hydra_attribute_id] }
20
-
21
- super
22
- end
23
-
24
- def all_models
25
- unless @full_loaded
26
- (hydra_attribute_ids - target.map(&:hydra_attribute_id)).each do |hydra_attribute_id|
27
- build(hydra_attribute_id: hydra_attribute_id)
28
- end
29
- @full_loaded = true
30
- end
31
- hydra_set_target
32
- end
33
-
34
- def save
35
- changed = false
36
- all_models.each do |model|
37
- model.entity_id = owner.id
38
- model.save
39
- changed = true unless model.previous_changes.blank?
40
- end
41
- changed
42
- end
43
-
44
- def lock!(white_list = [])
45
- @white_list = Array(white_list)
46
- loaded!
47
- end
48
-
49
- def hydra_attributes
50
- if @white_list
51
- @hydra_attributes ||= owner.class.hydra_set_attributes_for_backend_type(owner.hydra_set_id, reflection.backend_type)
52
- else
53
- owner.class.hydra_set_attributes_for_backend_type(owner.hydra_set_id, reflection.backend_type)
54
- end
55
- end
56
-
57
- def hydra_attribute_ids
58
- if @white_list
59
- @hydra_attribute_ids ||= owner.class.hydra_set_attribute_ids_for_backend_type(owner.hydra_set_id, reflection.backend_type) & @white_list
60
- else
61
- owner.class.hydra_set_attribute_ids_for_backend_type(owner.hydra_set_id, reflection.backend_type)
62
- end
63
- end
64
-
65
- def hydra_attribute_names
66
- if @white_list
67
- hydra_attributes.map(&:name)
68
- else
69
- owner.class.hydra_set_attribute_names_for_backend_type(owner.hydra_set_id, reflection.backend_type)
70
- end
71
- end
72
-
73
- def hydra_set_target
74
- @hydra_set_target ||= target.select do |model|
75
- hydra_attribute_ids.include?(model.hydra_attribute_id)
76
- end
77
- end
78
-
79
- def clear_cache!
80
- @hydra_attributes = nil
81
- @hydra_attribute_ids = nil
82
- @full_loaded = nil
83
- @hydra_set_target = nil
84
- end
85
-
86
- private
87
-
88
- # Optimized method
89
- # Remove unnecessary callbacks
90
- def add_to_target(record)
91
- @target << record
92
- record
93
- end
94
-
95
- # Optimized method
96
- # Attributes are written via low level function without additional checks
97
- def build_record(options, _)
98
- reflection.klass.new do |record|
99
- unless options.has_key?(:value)
100
- options[:value] = owner.class.hydra_attribute(options[:hydra_attribute_id]).default_value
101
- end
102
-
103
- record.send :write_attribute, 'id', options[:id]
104
- record.send :write_attribute, 'entity_id', owner.id
105
- record.send :write_attribute, 'hydra_attribute_id', options[:hydra_attribute_id]
106
- record.send :write_attribute, 'value', options[:value]
107
-
108
- hydra_set_target << record unless hydra_set_target.include?(record)
109
- end
110
- end
111
- end
112
- end
113
- end
@@ -1,16 +0,0 @@
1
- module HydraAttribute
2
- module ActiveRecord
3
- class Reflection < ::ActiveRecord::Reflection::AssociationReflection
4
-
5
- # Return custom association class
6
- # which is optimized to load hydra attributes
7
- def association_class
8
- Association
9
- end
10
-
11
- def backend_type
12
- @backend_type ||= SUPPORTED_BACKEND_TYPES.find { |type| type == klass.model_name.demodulize.underscore.split('_')[1] }
13
- end
14
- end
15
- end
16
- end
@@ -1,69 +0,0 @@
1
- module HydraAttribute
2
- class AssociationBuilder
3
- attr_reader :klass, :type
4
-
5
- def initialize(klass, type)
6
- @klass, @type = klass, type
7
- end
8
-
9
- class << self
10
- def build(klass, type)
11
- new(klass, type).build
12
- end
13
-
14
- def model_name(klass, type)
15
- "Hydra#{type.to_s.capitalize}#{klass.model_name}"
16
- end
17
-
18
- def class_name(klass, type)
19
- "::HydraAttribute::#{model_name(klass, type)}"
20
- end
21
-
22
- def table_name(klass, type)
23
- "hydra_#{type}_#{klass.table_name}"
24
- end
25
-
26
- def association_name(type)
27
- "hydra_#{type}_values".to_sym
28
- end
29
- end
30
-
31
- def build
32
- create_value_model
33
- add_association
34
- end
35
-
36
- def create_value_model
37
- value_model = ::HydraAttribute.const_set(model_name, Class.new(::ActiveRecord::Base))
38
- value_model.table_name = table_name
39
- value_model.belongs_to :entity, class_name: klass.model_name, autosave: false
40
- value_model.belongs_to :hydra_attribute, class_name: 'HydraAttribute::HydraAttribute'
41
-
42
- value_model.class_eval <<-EOS, __FILE__, __LINE__ + 1
43
- def hydra_attribute_name
44
- #{klass.model_name}.hydra_attribute(hydra_attribute_id).name
45
- end
46
- EOS
47
- end
48
-
49
- def add_association
50
- klass.has_many association_name, class_name: class_name, foreign_key: :entity_id, autosave: false
51
- end
52
-
53
- def model_name
54
- self.class.model_name(klass, type)
55
- end
56
-
57
- def class_name
58
- self.class.class_name(klass, type)
59
- end
60
-
61
- def table_name
62
- self.class.table_name(klass, type)
63
- end
64
-
65
- def association_name
66
- self.class.association_name(type)
67
- end
68
- end
69
- end