Class: RailsStuff::Statusable::MappedBuilder

Inherits:
Builder
  • Object
show all
Defined in:
lib/rails_stuff/statusable.rb

Overview

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

Instance Attribute Summary collapse

Attributes inherited from Builder

#field, #model, #options, #prefix, #statuses, #suffix

Instance Method Summary collapse

Methods inherited from Builder

#define_method, #define_scope, #generate, #generate_class_method, #select_options_helper, #status_accessor, #translation_helpers

Constructor Details

#initializeMappedBuilder

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

#mappingObject (readonly)

Returns the value of attribute mapping.



187
188
189
# File 'lib/rails_stuff/statusable.rb', line 187

def mapping
  @mapping
end

#statuses_listObject (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_readerObject



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_scopeObject

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_writerObject



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

#validationsObject



195
196
197
198
# File 'lib/rails_stuff/statusable.rb', line 195

def validations
  model.validates_inclusion_of field,
    {in: statuses.values}.merge!(options.fetch(:validate, {}))
end

#value_methodsObject



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