Class: RailsStuff::Statusable::MappedBuilder
- Defined in:
- lib/rails_stuff/statusable/mapped_builder.rb
Overview
Generates methods and scopes when status names are mapped to internal values.
Instance Attribute Summary
Attributes inherited from Builder
#helper, #options, #prefix, #suffix
Instance Method Summary collapse
- #each_status(&block) ⇒ Object
- #field_reader ⇒ Object
-
#field_scope ⇒ Object
Scope with given status.
- #field_writer ⇒ Object
Methods inherited from Builder
#field_accessor, #generate, #initialize, #status_method_name, #translation_helpers, #validations, #value_accessor, #value_accessors, #value_scopes
Constructor Details
This class inherits a constructor from RailsStuff::Statusable::Builder
Instance Method Details
#each_status(&block) ⇒ Object
10 11 12 |
# File 'lib/rails_stuff/statusable/mapped_builder.rb', line 10 def each_status(&block) mapping.each(&block) end |
#field_reader ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rails_stuff/statusable/mapped_builder.rb', line 21 def field_reader field = self.field helper = self.helper # Returns status name. define_method field do |original = false| val = super() original || !val ? val : helper.unmap(val) end # Status as symbol. define_method "#{field}_sym" do val = public_send(field) val && val.to_sym end end |
#field_scope ⇒ Object
Scope with given status. Useful for has_scope.
15 16 17 18 19 |
# File 'lib/rails_stuff/statusable/mapped_builder.rb', line 15 def field_scope field = self.field helper = self.helper define_scope "with_#{field}", ->(status) { where(field => helper.map(status)) } end |
#field_writer ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/rails_stuff/statusable/mapped_builder.rb', line 38 def field_writer helper = self.helper # Make field accept sympbols. define_method "#{field}=" do |val| val = val.to_s if val.is_a?(Symbol) super(helper.map(val)) end end |