class RailsStuff::Statusable::Builder
Basic builder for statuses list. Generates methods and scopes.
Attributes
helper[R]
options[R]
prefix[R]
suffix[R]
Public Class Methods
new(helper, **options)
click to toggle source
# File lib/rails_stuff/statusable/builder.rb, line 9 def initialize(helper, **options) @helper = helper @options = options @prefix = options[:prefix] @suffix = options[:suffix] end
Public Instance Methods
each_status() { |x, x| ... }
click to toggle source
Yields every status with it's database value into block.
# File lib/rails_stuff/statusable/builder.rb, line 36 def each_status list.each { |x| yield x, x.to_s } end
field_accessor()
click to toggle source
# File lib/rails_stuff/statusable/builder.rb, line 83 def field_accessor field_reader field_writer end
field_reader()
click to toggle source
Status as symbol.
# File lib/rails_stuff/statusable/builder.rb, line 97 def field_reader field = self.field define_method "#{field}_sym" do val = self[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/builder.rb, line 46 def field_scope field = self.field define_scope "with_#{field}", ->(status) { where(field => status) } end
field_writer()
click to toggle source
Make field accept sympbols.
Calls superclass method
# File lib/rails_stuff/statusable/builder.rb, line 89 def field_writer define_method "#{field}=" do |val| val = val.to_s if val.is_a?(Symbol) super(val) end end
generate()
click to toggle source
# File lib/rails_stuff/statusable/builder.rb, line 16 def generate validations if options.fetch(:validate, true) field_accessor field_scope value_scopes value_accessors translation_helpers end
status_method_name(status)
click to toggle source
Wraps status name with prefix and suffix.
# File lib/rails_stuff/statusable/builder.rb, line 41 def status_method_name(status) "#{prefix}#{status}#{suffix}" end
translation_helpers()
click to toggle source
# File lib/rails_stuff/statusable/builder.rb, line 105 def translation_helpers field = self.field define_method "#{field}_name" do val = send(field) self.class.t(".#{field}_name.#{val}") if val end end
valid_list()
click to toggle source
Field reader returns string, so we stringify list for validation.
# File lib/rails_stuff/statusable/builder.rb, line 31 def valid_list list.map(&:to_s) end
validations()
click to toggle source
# File lib/rails_stuff/statusable/builder.rb, line 25 def validations model.validates_inclusion_of field, {in: valid_list}.merge!(options.fetch(:validate, {})) end
value_accessor(status, value)
click to toggle source
Generates methods for specific value.
# File lib/rails_stuff/statusable/builder.rb, line 68 def value_accessor(status, value) field = self.field # Shortcut to check status. define_method "#{status_method_name(status)}?" do # Access raw value, 'cause reader can be overriden. self[field] == value end # Shortcut to update status. define_method "#{status_method_name(status)}!" do update!(field => value) end end
value_accessors()
click to toggle source
Status accessors for every status.
# File lib/rails_stuff/statusable/builder.rb, line 52 def value_accessors each_status do |status, value| value_accessor status, value end end
value_scopes()
click to toggle source
Scopes for every status.
# File lib/rails_stuff/statusable/builder.rb, line 59 def value_scopes field = self.field each_status do |status, value| define_scope status_method_name(status), -> { where(field => value) } define_scope "not_#{status_method_name(status)}", -> { where.not(field => value) } end end