-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenumerations.rb
172 lines (150 loc) · 5.06 KB
/
enumerations.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require 'active_support'
require 'active_support/concern'
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/string/inflections'
require 'enumerations/configuration'
require 'enumerations/version'
require 'enumerations/base'
require 'enumerations/reflection'
require 'enumerations/errors'
module Enumerations
extend ActiveSupport::Concern
included do
class_attribute :_enumerations
self._enumerations = []
end
module ClassMethods
# Create an enumeration for the symbol <tt>name</tt>.
# Options include <tt>foreign_key</tt> attribute and <tt>class_name</tt>
#
# Example:
#
# class User < ActiveRecord::Base
# enumeration :role
# end
#
# user.role => #<Enumerations::Value: @base=Role, @symbol=:admin...>
#
# user.role = Role.staff
#
def enumeration(name, options = {})
reflection = Reflection.new(name, options)
add_enumeration(reflection)
end
# Output all the enumerations that this model has defined
# Returns an array of Reflection objects for all the
# enumerations in the class.
#
# Example:
#
# User.reflect_on_all_enumerations => # [
# #<Enumerations::Reflection: @name=:role...>,
# #<Enumerations::Reflection: @name=:status...>
# ]
#
def reflect_on_all_enumerations
_enumerations
end
def fetch_foreign_key_values(reflection, *symbols)
symbols.flatten.map do |symbol|
enumeration_value = reflection.enumerator_class.find(symbol)
enumeration_value &&
enumeration_value.send(reflection.enumerator_class.primary_key || :symbol)
end
end
private
def add_enumeration(reflection)
define_getter_method(reflection)
define_setter_method(reflection)
define_bang_methods(reflection)
define_scopes_for_each_enumeration_value(reflection)
define_enumeration_scope(reflection)
self._enumerations += [reflection]
end
# Getter for belongs_to
#
# Example:
#
# user.role = Role.admin
# user.role => #<Enumerations::Value: @base=Role, @symbol=:admin...>
#
def define_getter_method(reflection)
define_method(reflection.name) do
reflection.enumerator_class.find(self[reflection.foreign_key]) || self[reflection.foreign_key]
end
end
# Setter for belongs_to
#
# Example:
#
# user.role = Role.admin
#
def define_setter_method(reflection)
define_method("#{reflection.name}=") do |other|
enumeration_value = reflection.enumerator_class.find(other)
if other.present? && enumeration_value.nil?
raise Enumerations::InvalidValueError if Enumerations.configuration.raise_invalid_value_error
self[reflection.foreign_key] = other
else
self[reflection.foreign_key] =
enumeration_value &&
enumeration_value.send(reflection.enumerator_class.primary_key || :symbol)
end
end
end
# Add bang methods for setting all enumeration values.
# All methods are prefixed with enumeration name.
#
# Example:
#
# user.role_admin!
# user.role => #<Enumerations::Value: @base=Role, @symbol=:admin...>
#
def define_bang_methods(reflection)
reflection.enumerator_class.all.each do |enumeration|
define_method("#{reflection.name}_#{enumeration.to_sym}!") do
send("#{reflection.name}=", enumeration)
end
end
end
# Scopes for enumerated ActiveRecord model.
# Format of scope name is with_#{enumeration_name}_#{enumeration_value_name}.
#
# Example:
#
# User.with_role_admin => <#ActiveRecord::Relation []>
# User.with_role_editor => <#ActiveRecord::Relation []>
#
def define_scopes_for_each_enumeration_value(reflection)
reflection.enumerator_class.all.each do |enumeration|
foreign_key = enumeration.send(reflection.enumerator_class.primary_key || :symbol)
scope("with_#{reflection.name}_#{enumeration.symbol}",
-> { where(reflection.foreign_key => foreign_key) })
scope("without_#{reflection.name}_#{enumeration.symbol}",
-> { where.not(reflection.foreign_key => foreign_key) })
end
end
# Scope for enumerated ActiveRecord model.
# Format of scope name is with_#{enumeration_name}(*enumerations).
#
# Example:
#
# User.with_role(:admin) => <#ActiveRecord::Relation []>
# User.with_role(:admin, Role.editor) => <#ActiveRecord::Relation []>
#
def define_enumeration_scope(reflection)
scope("with_#{reflection.name}",
lambda do |*symbols|
where(reflection.foreign_key => fetch_foreign_key_values(reflection, symbols))
end)
scope("without_#{reflection.name}",
lambda do |*symbols|
where.not(reflection.foreign_key => fetch_foreign_key_values(reflection, symbols))
end)
end
end
end
# Extend ActiveRecord with Enumeration capabilites
ActiveSupport.on_load(:active_record) do
include Enumerations
end