hydra_attribute 0.3.2 → 0.4.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +1 -1
- data/README.md +7 -0
- data/features/entity/create.feature +128 -0
- data/features/entity/destroy.feature +111 -0
- data/features/entity/new.feature +121 -0
- data/features/entity/update.feature +147 -0
- data/features/{attributes → hydra_attribute}/create.feature +7 -7
- data/features/{attributes → hydra_attribute}/destroy.feature +2 -4
- data/features/{attributes → hydra_attribute}/update.feature +10 -10
- data/features/hydra_set/destroy.feature +31 -0
- data/features/migrations/create_and_drop.feature +165 -0
- data/features/migrations/migrate_and_rollback.feature +211 -0
- data/features/relation/query_methods/group.feature +42 -0
- data/features/relation/query_methods/order.feature +67 -0
- data/features/relation/query_methods/reorder.feature +29 -0
- data/features/relation/query_methods/reverse_order.feature +29 -0
- data/features/relation/query_methods/select.feature +50 -0
- data/features/relation/query_methods/where.feature +70 -0
- data/features/step_definitions/connections.rb +65 -0
- data/features/step_definitions/model_steps.rb +79 -6
- data/features/step_definitions/query_methods.rb +3 -3
- data/features/step_definitions/record_steps.rb +3 -4
- data/features/support/env.rb +17 -3
- data/features/support/world.rb +15 -10
- data/gemfiles/3.1.gemfile.lock +15 -15
- data/gemfiles/3.2.gemfile.lock +15 -15
- data/lib/hydra_attribute/active_record/association.rb +74 -38
- data/lib/hydra_attribute/active_record/association_preloader.rb +49 -49
- data/lib/hydra_attribute/active_record/attribute_methods.rb +37 -85
- data/lib/hydra_attribute/active_record/migration.rb +2 -2
- data/lib/hydra_attribute/active_record/reflection.rb +1 -1
- data/lib/hydra_attribute/active_record/relation/query_methods.rb +8 -7
- data/lib/hydra_attribute/active_record/relation.rb +1 -0
- data/lib/hydra_attribute/active_record.rb +20 -12
- data/lib/hydra_attribute/association_builder.rb +1 -2
- data/lib/hydra_attribute/builder.rb +7 -8
- data/lib/hydra_attribute/entity_callbacks.rb +12 -32
- data/lib/hydra_attribute/hydra_attribute.rb +25 -16
- data/lib/hydra_attribute/hydra_attribute_methods.rb +85 -0
- data/lib/hydra_attribute/hydra_methods.rb +123 -0
- data/lib/hydra_attribute/hydra_set.rb +36 -0
- data/lib/hydra_attribute/hydra_set_methods.rb +39 -0
- data/lib/hydra_attribute/hydra_value_methods.rb +14 -0
- data/lib/hydra_attribute/memoize.rb +37 -0
- data/lib/hydra_attribute/migrator.rb +100 -51
- data/lib/hydra_attribute/railtie.rb +1 -3
- data/lib/hydra_attribute/version.rb +1 -1
- data/lib/hydra_attribute.rb +7 -1
- data/spec/hydra_attribute_methods_spec.rb +458 -0
- data/spec/hydra_attribute_spec.rb +19 -0
- data/spec/hydra_methods_spec.rb +457 -0
- data/spec/hydra_set_methods_spec.rb +203 -0
- data/spec/hydra_set_spec.rb +19 -0
- data/spec/memoize_spec.rb +95 -0
- data/spec/spec_helper.rb +42 -2
- metadata +71 -43
- data/features/create.feature +0 -47
- data/features/define.feature +0 -38
- data/features/destroy.feature +0 -102
- data/features/query_methods/group.feature +0 -42
- data/features/query_methods/order.feature +0 -99
- data/features/query_methods/select.feature +0 -50
- data/features/query_methods/where.feature +0 -70
- data/features/support/schema.rb +0 -79
- data/features/update.feature +0 -114
@@ -0,0 +1,85 @@
|
|
1
|
+
module HydraAttribute
|
2
|
+
module HydraAttributeMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
extend Memoize
|
7
|
+
|
8
|
+
def hydra_attributes
|
9
|
+
HydraAttribute.where(entity_type: base_class.model_name)
|
10
|
+
end
|
11
|
+
hydra_memoize :hydra_attributes
|
12
|
+
|
13
|
+
def hydra_attribute(identifier)
|
14
|
+
hydra_attributes.find do |hydra_attribute|
|
15
|
+
hydra_attribute.id == identifier || hydra_attribute.name == identifier
|
16
|
+
end
|
17
|
+
end
|
18
|
+
hydra_memoize :hydra_attribute
|
19
|
+
|
20
|
+
def hydra_attribute_backend_types
|
21
|
+
hydra_attributes.map(&:backend_type).uniq
|
22
|
+
end
|
23
|
+
hydra_memoize :hydra_attribute_backend_types
|
24
|
+
|
25
|
+
%w(id name).each do |prefix|
|
26
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
27
|
+
def hydra_attribute_#{prefix}s
|
28
|
+
hydra_attributes.map(&:#{prefix})
|
29
|
+
end
|
30
|
+
hydra_memoize :hydra_attribute_#{prefix}s
|
31
|
+
EOS
|
32
|
+
end
|
33
|
+
|
34
|
+
def hydra_attributes_by_backend_type
|
35
|
+
hydra_attributes.group_by(&:backend_type)
|
36
|
+
end
|
37
|
+
hydra_memoize :hydra_attributes_by_backend_type
|
38
|
+
|
39
|
+
%w(id name).each do |prefix|
|
40
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
41
|
+
def hydra_attribute_#{prefix}s_by_backend_type
|
42
|
+
hydra_attributes.each_with_object({}) do |hydra_attribute, object|
|
43
|
+
object[hydra_attribute.backend_type] ||= []
|
44
|
+
object[hydra_attribute.backend_type] << hydra_attribute.#{prefix}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
hydra_memoize :hydra_attribute_#{prefix}s_by_backend_type
|
48
|
+
EOS
|
49
|
+
end
|
50
|
+
|
51
|
+
def hydra_attributes_for_backend_type(backend_type)
|
52
|
+
hydra_attributes = hydra_attributes_by_backend_type[backend_type]
|
53
|
+
hydra_attributes.nil? ? [] : hydra_attributes
|
54
|
+
end
|
55
|
+
|
56
|
+
%w(id name).each do |prefix|
|
57
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
58
|
+
def hydra_attribute_#{prefix}s_for_backend_type(backend_type)
|
59
|
+
values = hydra_attribute_#{prefix}s_by_backend_type[backend_type]
|
60
|
+
values.nil? ? [] : values
|
61
|
+
end
|
62
|
+
EOS
|
63
|
+
end
|
64
|
+
|
65
|
+
def clear_hydra_attribute_cache!
|
66
|
+
[
|
67
|
+
:@hydra_attributes,
|
68
|
+
:@hydra_attribute,
|
69
|
+
:@hydra_attribute_ids,
|
70
|
+
:@hydra_attribute_names,
|
71
|
+
:@hydra_attribute_backend_types,
|
72
|
+
:@hydra_attributes_by_backend_type,
|
73
|
+
:@hydra_attribute_ids_by_backend_type,
|
74
|
+
:@hydra_attribute_names_by_backend_type,
|
75
|
+
].each do |variable|
|
76
|
+
remove_instance_variable(variable) if instance_variable_defined?(variable)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def hydra_attribute?(name)
|
82
|
+
self.class.hydra_attribute_names.include?(name.to_s)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
module HydraAttribute
|
2
|
+
module HydraMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
extend Memoize
|
5
|
+
|
6
|
+
include HydraSetMethods
|
7
|
+
include HydraAttributeMethods
|
8
|
+
include HydraValueMethods
|
9
|
+
|
10
|
+
included do
|
11
|
+
alias_method_chain :write_attribute, :hydra_set_id
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
extend Memoize
|
16
|
+
|
17
|
+
def hydra_set_attributes(hydra_set_id)
|
18
|
+
hydra_set = hydra_set(hydra_set_id)
|
19
|
+
hydra_set.nil? ? hydra_attributes : hydra_set.hydra_attributes
|
20
|
+
end
|
21
|
+
|
22
|
+
%w(id name backend_type).each do |prefix|
|
23
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
24
|
+
def hydra_set_attribute_#{prefix}s(hydra_set_id)
|
25
|
+
hydra_set_attributes(hydra_set_id).map(&:#{prefix})
|
26
|
+
end
|
27
|
+
hydra_memoize :hydra_set_attribute_#{prefix}s
|
28
|
+
EOS
|
29
|
+
end
|
30
|
+
|
31
|
+
def hydra_set_attributes_by_backend_type(hydra_set_id)
|
32
|
+
hydra_set_attributes(hydra_set_id).group_by(&:backend_type)
|
33
|
+
end
|
34
|
+
hydra_memoize :hydra_set_attributes_by_backend_type
|
35
|
+
|
36
|
+
%w(id name).each do |prefix|
|
37
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
38
|
+
def hydra_set_attribute_#{prefix}s_by_backend_type(hydra_set_id)
|
39
|
+
hydra_set_attributes(hydra_set_id).each_with_object({}) do |hydra_attribute, object|
|
40
|
+
object[hydra_attribute.backend_type] ||= []
|
41
|
+
object[hydra_attribute.backend_type] << hydra_attribute.#{prefix}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
hydra_memoize :hydra_set_attribute_#{prefix}s_by_backend_type
|
45
|
+
EOS
|
46
|
+
end
|
47
|
+
|
48
|
+
def hydra_set_attributes_for_backend_type(hydra_set_id, backend_type)
|
49
|
+
hydra_attributes = hydra_set_attributes_by_backend_type(hydra_set_id)[backend_type]
|
50
|
+
hydra_attributes.nil? ? [] : hydra_attributes
|
51
|
+
end
|
52
|
+
|
53
|
+
%w(id name).each do |prefix|
|
54
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
55
|
+
def hydra_set_attribute_#{prefix}s_for_backend_type(hydra_set_id, backend_type)
|
56
|
+
values = hydra_set_attribute_#{prefix}s_by_backend_type(hydra_set_id)[backend_type]
|
57
|
+
values.nil? ? [] : values
|
58
|
+
end
|
59
|
+
EOS
|
60
|
+
end
|
61
|
+
|
62
|
+
def clear_hydra_method_cache!
|
63
|
+
clear_hydra_set_cache!
|
64
|
+
clear_hydra_attribute_cache!
|
65
|
+
clear_hydra_value_cache!
|
66
|
+
|
67
|
+
[
|
68
|
+
:@hydra_set_attributes,
|
69
|
+
:@hydra_set_attribute_ids,
|
70
|
+
:@hydra_set_attribute_names,
|
71
|
+
:@hydra_set_attribute_backend_types,
|
72
|
+
:@hydra_set_attributes_by_backend_type,
|
73
|
+
:@hydra_set_attribute_ids_by_backend_type,
|
74
|
+
:@hydra_set_attribute_names_by_backend_type
|
75
|
+
].each do |variable|
|
76
|
+
remove_instance_variable(variable) if instance_variable_defined?(variable)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def hydra_attributes
|
82
|
+
hydra_value_models.inject({}) do |hydra_attributes, model|
|
83
|
+
hydra_attributes[model.hydra_attribute_name] = model.value
|
84
|
+
hydra_attributes
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
%w(ids names backend_types).each do |prefix|
|
89
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
90
|
+
def hydra_attribute_#{prefix}
|
91
|
+
self.class.hydra_set_attribute_#{prefix}(hydra_set_id)
|
92
|
+
end
|
93
|
+
EOS
|
94
|
+
end
|
95
|
+
|
96
|
+
def hydra_value_model(identifier)
|
97
|
+
hydra_attribute = self.class.hydra_attribute(identifier)
|
98
|
+
if hydra_attribute
|
99
|
+
association = hydra_value_association(hydra_attribute.backend_type)
|
100
|
+
association.find_model_or_build(hydra_attribute_id: hydra_attribute.id)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
hydra_memoize :hydra_value_model
|
104
|
+
|
105
|
+
def hydra_value_models
|
106
|
+
self.class.hydra_set_attribute_backend_types(hydra_set_id).inject([]) do |models, backend_type|
|
107
|
+
models + hydra_value_association(backend_type).all_models
|
108
|
+
end
|
109
|
+
end
|
110
|
+
hydra_memoize :hydra_value_models
|
111
|
+
|
112
|
+
private
|
113
|
+
def write_attribute_with_hydra_set_id(attr_name, value)
|
114
|
+
if attr_name.to_s == 'hydra_set_id'
|
115
|
+
self.class.hydra_attribute_backend_types.each do |backend_type|
|
116
|
+
hydra_value_association(backend_type).clear_cache!
|
117
|
+
end
|
118
|
+
remove_instance_variable(:@hydra_value_models) if instance_variable_defined?(:@hydra_value_models)
|
119
|
+
end
|
120
|
+
write_attribute_without_hydra_set_id(attr_name, value)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'active_support/core_ext/object/with_options'
|
2
|
+
|
3
|
+
module HydraAttribute
|
4
|
+
class HydraSet < ActiveRecord::Base
|
5
|
+
self.table_name = 'hydra_sets'
|
6
|
+
|
7
|
+
attr_accessible :name
|
8
|
+
|
9
|
+
has_and_belongs_to_many :hydra_attributes, join_table: 'hydra_attribute_sets', class_name: 'HydraAttribute::HydraAttribute', conditions: proc { {hydra_attributes: {entity_type: entity_type}} }
|
10
|
+
|
11
|
+
with_options presence: true do |klass|
|
12
|
+
klass.validates :entity_type, inclusion: { in: lambda { |attr| [(attr.entity_type.constantize.name rescue nil)] } }
|
13
|
+
klass.validates :name, uniqueness: { scope: :entity_type }
|
14
|
+
end
|
15
|
+
|
16
|
+
after_destroy :detach_entities
|
17
|
+
after_commit :clear_entity_cache
|
18
|
+
|
19
|
+
# @COMPATIBILITY with 3.1.x association module is directly added to the class instead of including module
|
20
|
+
def hydra_attributes_with_clearing_cache=(value)
|
21
|
+
self.hydra_attributes_without_clearing_cache = value
|
22
|
+
clear_entity_cache
|
23
|
+
value
|
24
|
+
end
|
25
|
+
alias_method_chain :hydra_attributes=, :clearing_cache
|
26
|
+
|
27
|
+
private
|
28
|
+
def clear_entity_cache
|
29
|
+
entity_type.constantize.clear_hydra_method_cache!
|
30
|
+
end
|
31
|
+
|
32
|
+
def detach_entities
|
33
|
+
entity_type.constantize.where(hydra_set_id: id).update_all(hydra_set_id: nil)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module HydraAttribute
|
2
|
+
class MissingAttributeInHydraSetError < NoMethodError
|
3
|
+
end
|
4
|
+
|
5
|
+
module HydraSetMethods
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
extend Memoize
|
10
|
+
|
11
|
+
def hydra_sets
|
12
|
+
HydraSet.where(entity_type: base_class.model_name)
|
13
|
+
end
|
14
|
+
hydra_memoize :hydra_sets
|
15
|
+
|
16
|
+
def hydra_set(identifier)
|
17
|
+
hydra_sets.find do |hydra_set|
|
18
|
+
hydra_set.id == identifier || hydra_set.name == identifier
|
19
|
+
end
|
20
|
+
end
|
21
|
+
hydra_memoize :hydra_set
|
22
|
+
|
23
|
+
%w(id name).each do |prefix|
|
24
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
25
|
+
def hydra_set_#{prefix}s
|
26
|
+
hydra_sets.map(&:#{prefix})
|
27
|
+
end
|
28
|
+
hydra_memoize :hydra_set_#{prefix}s
|
29
|
+
EOS
|
30
|
+
end
|
31
|
+
|
32
|
+
def clear_hydra_set_cache!
|
33
|
+
[:@hydra_sets, :@hydra_set, :@hydra_set_ids, :@hydra_set_names].each do |variable|
|
34
|
+
remove_instance_variable(variable) if instance_variable_defined?(variable)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module HydraAttribute
|
2
|
+
module HydraValueMethods
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def clear_hydra_value_cache!
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def hydra_value_association(backend_type)
|
11
|
+
association(::HydraAttribute::AssociationBuilder.association_name(backend_type))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module HydraAttribute
|
2
|
+
module Memoize
|
3
|
+
def hydra_memoize(*methods)
|
4
|
+
methods.each do |method_name|
|
5
|
+
bound_method = instance_method(method_name)
|
6
|
+
alias_method "unmemoized_#{method_name}", method_name
|
7
|
+
|
8
|
+
if bound_method.arity.abs == 0
|
9
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
10
|
+
def #{method_name}
|
11
|
+
@#{method_name} = unmemoized_#{method_name} unless instance_variable_defined?(:@#{method_name})
|
12
|
+
@#{method_name}
|
13
|
+
end
|
14
|
+
EOS
|
15
|
+
else
|
16
|
+
args = 1.upto(bound_method.arity.abs).map { |i| "a#{i}" }
|
17
|
+
keys = 1.upto(bound_method.arity.abs).map { |i| "k#{i}" }
|
18
|
+
|
19
|
+
call = args.inject("@#{method_name}") do |hash_call, arg|
|
20
|
+
"#{hash_call}[#{arg}]"
|
21
|
+
end
|
22
|
+
|
23
|
+
hash = keys.reverse.inject("unmemoized_#{method_name}(#{keys.join(', ')})") do |code, key|
|
24
|
+
"Hash.new { |hash, #{key}| hash[#{key}] = #{code} }"
|
25
|
+
end
|
26
|
+
|
27
|
+
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
28
|
+
def #{method_name}(#{args.join(', ')}) # def method(a1)
|
29
|
+
@#{method_name} ||= #{hash} # @method ||= Hash.new { |hash, key1| hash[key1] = unmemoized_method(key1) }
|
30
|
+
#{call} # @method[a1]
|
31
|
+
end # end
|
32
|
+
EOS
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -6,7 +6,7 @@ module HydraAttribute
|
|
6
6
|
end
|
7
7
|
|
8
8
|
class << self
|
9
|
-
%w(create
|
9
|
+
%w(create drop migrate rollback).each do |method|
|
10
10
|
class_eval <<-EOS, __FILE__, __LINE__ + 1
|
11
11
|
def #{method}(migration, *args, &block)
|
12
12
|
new(migration).#{method}(*args, &block)
|
@@ -18,90 +18,139 @@ module HydraAttribute
|
|
18
18
|
def create(name, options = {}, &block)
|
19
19
|
create_entity(name, options, &block)
|
20
20
|
create_attribute unless attribute_exists?
|
21
|
+
create_set unless set_exists?
|
21
22
|
create_values(name)
|
22
23
|
end
|
23
24
|
|
24
25
|
def drop(name)
|
25
26
|
drop_values(name)
|
26
|
-
|
27
|
+
unless values_exists?
|
28
|
+
drop_set
|
29
|
+
drop_attribute
|
30
|
+
end
|
27
31
|
drop_entity(name)
|
28
32
|
end
|
29
33
|
|
30
|
-
def migrate(name)
|
34
|
+
def migrate(name, options = {}, &block)
|
35
|
+
migrate_entity(name, options, &block)
|
31
36
|
create_attribute unless attribute_exists?
|
37
|
+
create_set unless set_exists?
|
32
38
|
create_values(name)
|
33
39
|
end
|
34
40
|
|
35
41
|
def rollback(name)
|
36
42
|
drop_values(name)
|
37
|
-
|
43
|
+
unless values_exists?
|
44
|
+
drop_attribute
|
45
|
+
drop_set
|
46
|
+
end
|
47
|
+
rollback_entity(name)
|
38
48
|
end
|
39
49
|
|
40
50
|
private
|
51
|
+
def create_entity(name, options = {})
|
52
|
+
create_table name, options do |t|
|
53
|
+
t.integer :hydra_set_id, null: true
|
54
|
+
yield t if block_given?
|
55
|
+
end
|
56
|
+
add_index name, :hydra_set_id, unique: false, name: "#{name}_hydra_set_id_index"
|
57
|
+
end
|
41
58
|
|
42
|
-
|
43
|
-
|
44
|
-
|
59
|
+
def migrate_entity(name, options = {})
|
60
|
+
change_table name, options do |t|
|
61
|
+
t.integer :hydra_set_id, null: true
|
62
|
+
end
|
63
|
+
add_index name, :hydra_set_id, uniqie: false, name: "#{name}_hydra_set_id_index"
|
45
64
|
end
|
46
|
-
end
|
47
65
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
66
|
+
def create_attribute
|
67
|
+
create_table :hydra_attributes do |t|
|
68
|
+
t.string :entity_type, limit: 32, null: false
|
69
|
+
t.string :name, limit: 32, null: false
|
70
|
+
t.string :backend_type, limit: 16, null: false
|
71
|
+
t.string :default_value
|
72
|
+
t.boolean :white_list, null: false, default: false
|
73
|
+
t.datetime :created_at # @COMPATIBILITY with 3.1.x use "datetime" method instead of "timestamps" because Rails 3.1.x and 3.2.x have a different "null" value
|
74
|
+
t.datetime :updated_at # @COMPATIBILITY with 3.1.x
|
75
|
+
end
|
76
|
+
add_index :hydra_attributes, [:entity_type, :name], unique: true, name: 'hydra_attributes_index'
|
56
77
|
end
|
57
|
-
add_index :hydra_attributes, [:entity_type, :name], unique: true, name: 'hydra_attributes_index'
|
58
|
-
end
|
59
78
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
t.
|
79
|
+
def create_set
|
80
|
+
create_table :hydra_sets do |t|
|
81
|
+
t.string :entity_type, limit: 32, null: false
|
82
|
+
t.string :name, limit: 32, null: false
|
83
|
+
t.datetime :created_at # @COMPATIBILITY with 3.1.x
|
84
|
+
t.datetime :updated_at # @COMPATIBILITY with 3.1.x
|
85
|
+
end
|
86
|
+
add_index :hydra_sets, [:entity_type, :name], unique: true, name: 'hydra_sets_index'
|
87
|
+
|
88
|
+
create_table :hydra_attribute_sets, id: false do |t|
|
65
89
|
t.integer :hydra_attribute_id, null: false
|
66
|
-
t.
|
67
|
-
t.timestamps
|
90
|
+
t.integer :hydra_set_id, null: false
|
68
91
|
end
|
69
|
-
add_index
|
92
|
+
add_index :hydra_attribute_sets, [:hydra_attribute_id, :hydra_set_id], unique: true, name: 'hydra_attribute_sets_index'
|
70
93
|
end
|
71
|
-
end
|
72
94
|
|
73
|
-
|
74
|
-
|
75
|
-
|
95
|
+
def create_values(name)
|
96
|
+
SUPPORTED_BACKEND_TYPES.each do |type|
|
97
|
+
table_name = value_table(name, type)
|
98
|
+
create_table table_name do |t|
|
99
|
+
t.integer :entity_id, null: false
|
100
|
+
t.integer :hydra_attribute_id, null: false
|
101
|
+
t.send type, :value
|
102
|
+
t.datetime :created_at # @COMPATIBILITY with 3.1.x
|
103
|
+
t.datetime :updated_at # @COMPATIBILITY with 3.1.x
|
104
|
+
end
|
105
|
+
add_index table_name, [:entity_id, :hydra_attribute_id], unique: true, name: "#{table_name}_index"
|
106
|
+
end
|
107
|
+
end
|
76
108
|
|
77
|
-
|
78
|
-
|
79
|
-
|
109
|
+
def drop_entity(name)
|
110
|
+
drop_table(name)
|
111
|
+
end
|
80
112
|
|
81
|
-
|
82
|
-
|
83
|
-
drop_table(value_table(name, type))
|
113
|
+
def rollback_entity(name)
|
114
|
+
remove_column name, :hydra_set_id
|
84
115
|
end
|
85
|
-
end
|
86
116
|
|
87
|
-
|
88
|
-
|
89
|
-
|
117
|
+
def drop_attribute
|
118
|
+
drop_table(:hydra_attributes)
|
119
|
+
end
|
90
120
|
|
91
|
-
|
92
|
-
|
93
|
-
|
121
|
+
def drop_set
|
122
|
+
drop_table(:hydra_attribute_sets)
|
123
|
+
drop_table(:hydra_sets)
|
124
|
+
end
|
94
125
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
table.start_with?("hydra_#{type}_")
|
126
|
+
def drop_values(name)
|
127
|
+
SUPPORTED_BACKEND_TYPES.each do |type|
|
128
|
+
drop_table(value_table(name, type))
|
99
129
|
end
|
100
130
|
end
|
101
|
-
end
|
102
131
|
|
103
|
-
|
104
|
-
|
105
|
-
|
132
|
+
def value_table(name, type)
|
133
|
+
"hydra_#{type}_#{name}"
|
134
|
+
end
|
135
|
+
|
136
|
+
def attribute_exists?
|
137
|
+
table_exists?(:hydra_attributes)
|
138
|
+
end
|
139
|
+
|
140
|
+
def set_exists?
|
141
|
+
table_exists?(:hydra_sets)
|
142
|
+
end
|
143
|
+
|
144
|
+
def values_exists?
|
145
|
+
tables.any? do |table|
|
146
|
+
SUPPORTED_BACKEND_TYPES.any? do |type|
|
147
|
+
table.start_with?("hydra_#{type}_")
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def method_missing(symbol, *args, &block)
|
153
|
+
@migration.send(symbol, *args, &block)
|
154
|
+
end
|
106
155
|
end
|
107
156
|
end
|
@@ -2,9 +2,7 @@ module HydraAttribute
|
|
2
2
|
class Railtie < Rails::Railtie
|
3
3
|
initializer 'hydra_attribute.active_record' do
|
4
4
|
ActiveSupport.on_load :active_record do
|
5
|
-
|
6
|
-
|
7
|
-
::ActiveRecord::Migration.send :include, ::HydraAttribute::ActiveRecord::Migration
|
5
|
+
::ActiveRecord::Migration.send(:include, ::HydraAttribute::ActiveRecord::Migration)
|
8
6
|
end
|
9
7
|
end
|
10
8
|
end
|
data/lib/hydra_attribute.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module HydraAttribute
|
2
|
-
|
2
|
+
SUPPORTED_BACKEND_TYPES = %w(string text integer float boolean datetime).freeze
|
3
3
|
|
4
4
|
class << self
|
5
5
|
def config
|
@@ -18,8 +18,14 @@ require 'hydra_attribute/configuration'
|
|
18
18
|
require 'hydra_attribute/association_builder'
|
19
19
|
require 'hydra_attribute/builder'
|
20
20
|
require 'hydra_attribute/migrator'
|
21
|
+
require 'hydra_attribute/memoize'
|
21
22
|
require 'hydra_attribute/hydra_attribute'
|
23
|
+
require 'hydra_attribute/hydra_set'
|
22
24
|
require 'hydra_attribute/entity_callbacks'
|
25
|
+
require 'hydra_attribute/hydra_set_methods'
|
26
|
+
require 'hydra_attribute/hydra_value_methods'
|
27
|
+
require 'hydra_attribute/hydra_attribute_methods'
|
28
|
+
require 'hydra_attribute/hydra_methods'
|
23
29
|
require 'hydra_attribute/active_record'
|
24
30
|
require 'hydra_attribute/active_record/scoping'
|
25
31
|
require 'hydra_attribute/active_record/reflection'
|