custom_fields 1.0.0.beta.25 → 1.1.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,133 +0,0 @@
1
- module CustomFields
2
- module ProxyClassEnabler
3
-
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
-
8
- def self.to_klass_with_custom_fields(fields, parent, association_name)
9
- klass = self.current_klass_with_custom_fields(parent, association_name)
10
-
11
- # for debug purpose
12
- # if klass.nil?
13
- # puts "[#{association_name.to_s.gsub(/^_/, '').singularize} / #{parent.name rescue 'unknown'}] no klass found"
14
- # else
15
- # puts "[#{association_name.to_s.gsub(/^_/, '').singularize} / #{parent.name rescue 'unknown'}] klass nil ? #{klass.nil?} / current version ? #{klass.version.inspect} / parent ? #{self.custom_fields_version(parent, association_name)}" # for debug purpose
16
- # end
17
-
18
- if klass && klass.version != self.custom_fields_version(parent, association_name) # new version ?
19
- self.invalidate_proxy_class_with_custom_fields(parent, association_name)
20
- klass = nil
21
- end
22
-
23
- if klass.nil?
24
- klass = self.build_proxy_class_with_custom_fields(fields, parent, association_name)
25
-
26
- klass_name = self.klass_name_with_custom_fields(parent, association_name)
27
-
28
- Object.const_set(klass_name, klass)
29
- end
30
-
31
- klass
32
- end
33
-
34
- def self.custom_fields_version(parent, association_name)
35
- singular_name = association_name.to_s.gsub(/^_/, '').singularize
36
- parent.send(:"#{singular_name}_custom_fields_version")
37
- end
38
-
39
- def self.invalidate_proxy_class_with_custom_fields(parent, association_name)
40
- # puts "-> invalidate_proxy_class_with_custom_fields !!!!!" # for debug purpose
41
- klass_name = self.klass_name_with_custom_fields(parent, association_name)
42
-
43
- if Object.const_defined?(klass_name)
44
- Object.send(:remove_const, klass_name)
45
- end
46
- end
47
-
48
- def self.current_klass_with_custom_fields(parent, association_name)
49
- klass_name = self.klass_name_with_custom_fields(parent, association_name)
50
-
51
- Object.const_defined?(klass_name) ? Object.const_get(klass_name): nil
52
- end
53
-
54
- def self.klass_name_with_custom_fields(parent, association_name)
55
- "#{association_name.to_s.gsub(/^_/, '').singularize.camelize}#{parent.class.name.camelize}#{parent._id}"
56
- end
57
-
58
- def self.build_proxy_class_with_custom_fields(fields, parent, association_name)
59
- # puts "BUILDING PROXY CLASS #{association_name} / parent version #{self.custom_fields_version(parent, association_name)}" # for debug purpose
60
-
61
- (klass = Class.new(self)).class_eval <<-EOF
62
-
63
- cattr_accessor :custom_fields, :_parent, :association_name, :built_at, :version
64
-
65
- def self.model_name
66
- @_model_name ||= ActiveModel::Name.new(self.superclass)
67
- end
68
-
69
- def self.apply_custom_field(field)
70
- unless field.persisted?
71
- return unless field.valid?
72
- end
73
-
74
- self.custom_fields ||= []
75
-
76
- if self.lookup_custom_field(field._name).nil?
77
- self.custom_fields << field
78
- field.apply(self)
79
- end
80
- end
81
-
82
- def self.lookup_custom_field(name)
83
- self.custom_fields.detect { |f| f._name == name }
84
- end
85
-
86
- def self.custom_field_alias_to_name(value)
87
- self.custom_fields.detect { |f| f._alias == value }._name
88
- end
89
-
90
- def self.custom_field_name_to_alias(value)
91
- self.custom_fields.detect { |f| f._name == value }._alias
92
- end
93
-
94
- def self.hereditary?
95
- false
96
- end
97
-
98
- def custom_fields
99
- self.class.custom_fields
100
- end
101
-
102
- def aliased_attributes
103
- hash = { :created_at => self.created_at, :updated_at => self.updated_at } rescue {}
104
-
105
- (self.custom_fields || []).each do |field|
106
- case field.kind
107
- when 'file' then hash[field._alias] = self.send(field._name.to_sym).url
108
- else
109
- hash[field._alias] = self.send(field._name.to_sym)
110
- end
111
- end
112
-
113
- hash
114
- end
115
- EOF
116
-
117
- # copy scopes from the parent class
118
- klass.write_inheritable_attribute(:scopes, self.scopes)
119
-
120
- klass.association_name = association_name.to_sym
121
- klass._parent = parent
122
-
123
- [*fields].each { |field| klass.apply_custom_field(field) }
124
-
125
- klass.version = self.custom_fields_version(parent, association_name)
126
-
127
- klass
128
- end
129
-
130
- end
131
-
132
- end
133
- end