Class: RailsStuff::Statusable::MappedBuilder
- Defined in:
- lib/rails_stuff/statusable.rb
Overview
Generates methods and scopes when status names are mapped to internal values.
Instance Attribute Summary collapse
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
-
#statuses_list ⇒ Object
readonly
Returns the value of attribute statuses_list.
Attributes inherited from Builder
#field, #model, #options, #prefix, #statuses, #suffix
Instance Method Summary collapse
- #field_reader ⇒ Object
-
#field_scope ⇒ Object
Scope with given status.
- #field_writer ⇒ Object
-
#initialize ⇒ MappedBuilder
constructor
A new instance of MappedBuilder.
- #validations ⇒ Object
- #value_methods ⇒ Object
Methods inherited from Builder
#define_method, #define_scope, #generate, #generate_class_method, #select_options_helper, #status_accessor, #translation_helpers
Constructor Details
#initialize ⇒ MappedBuilder
Returns a new instance of MappedBuilder.
184 185 186 187 188 |
# File 'lib/rails_stuff/statusable.rb', line 184 def initialize(*) super @mapping = statuses.with_indifferent_access @statuses_list = statuses.keys end |
Instance Attribute Details
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
182 183 184 |
# File 'lib/rails_stuff/statusable.rb', line 182 def mapping @mapping end |
#statuses_list ⇒ Object (readonly)
Returns the value of attribute statuses_list.
182 183 184 |
# File 'lib/rails_stuff/statusable.rb', line 182 def statuses_list @statuses_list end |
Instance Method Details
#field_reader ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/rails_stuff/statusable.rb', line 215 def field_reader field = self.field inverse_mapping = statuses.stringify_keys.invert # Returns status name. define_method field do |mapped = false| val = super() return val unless mapped && val mapped = inverse_mapping[val] raise "Missing mapping for value #{val.inspect}" unless mapped mapped end # Status as symbol. define_method "#{field}_sym" do val = public_send(field, true) val && val.to_sym end end |
#field_scope ⇒ Object
Scope with given status. Useful for has_scope.
196 197 198 199 200 201 202 203 |
# File 'lib/rails_stuff/statusable.rb', line 196 def field_scope field = self.field mapping = self.mapping define_scope "with_#{field}", ->(status) do values = Array.wrap(status).map { |x| mapping.fetch(x, x) } where(field => values) end end |
#field_writer ⇒ Object
235 236 237 238 239 240 241 242 |
# File 'lib/rails_stuff/statusable.rb', line 235 def field_writer mapping = self.mapping # Make field accept sympbols. define_method "#{field}=" do |val| val = val.to_s if val.is_a?(Symbol) super(mapping.fetch(val, val)) end end |
#validations ⇒ Object
190 191 192 193 |
# File 'lib/rails_stuff/statusable.rb', line 190 def validations model.validates_inclusion_of field, {in: statuses.values}.merge!(.fetch(:validate, {})) end |
#value_methods ⇒ Object
205 206 207 208 209 210 211 212 213 |
# File 'lib/rails_stuff/statusable.rb', line 205 def value_methods field = self.field statuses.each do |status_name, value| # Scopes for every status. define_scope "#{prefix}#{status_name}#{suffix}", -> { where(field => value) } define_scope "not_#{prefix}#{status_name}#{suffix}", -> { where.not(field => value) } status_accessor status_name, value end end |