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.
189 190 191 192 193 |
# File 'lib/rails_stuff/statusable.rb', line 189 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.
187 188 189 |
# File 'lib/rails_stuff/statusable.rb', line 187 def mapping @mapping end |
#statuses_list ⇒ Object (readonly)
Returns the value of attribute statuses_list.
187 188 189 |
# File 'lib/rails_stuff/statusable.rb', line 187 def statuses_list @statuses_list end |
Instance Method Details
#field_reader ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/rails_stuff/statusable.rb', line 220 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.
201 202 203 204 205 206 207 208 |
# File 'lib/rails_stuff/statusable.rb', line 201 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
240 241 242 243 244 245 246 247 |
# File 'lib/rails_stuff/statusable.rb', line 240 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
195 196 197 198 |
# File 'lib/rails_stuff/statusable.rb', line 195 def validations model.validates_inclusion_of field, {in: statuses.values}.merge!(.fetch(:validate, {})) end |
#value_methods ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/rails_stuff/statusable.rb', line 210 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 |