class RailsStuff::Statusable::MappedBuilder

Generates methods and scopes when status names are mapped to internal values.

Public Instance Methods

each_status(&block) click to toggle source
# File lib/rails_stuff/statusable/mapped_builder.rb, line 10
def each_status(&block)
  mapping.each(&block)
end
field_reader() click to toggle source
# 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() click to toggle source

Scope with given status. Useful for has_scope.

# 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() click to toggle source
# 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