Class: RailsStuff::Statusable::Builder

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

Overview

Basic builder for statuses list. Generates methods and scopes.

Direct Known Subclasses

MappedBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(helper, **options) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
# File 'lib/rails_stuff/statusable/builder.rb', line 9

def initialize(helper, **options)
  @helper = helper
  @options = options
  @prefix = options[:prefix]
  @suffix = options[:suffix]
end

Instance Attribute Details

#helperObject (readonly)

Returns the value of attribute helper.



5
6
7
# File 'lib/rails_stuff/statusable/builder.rb', line 5

def helper
  @helper
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/rails_stuff/statusable/builder.rb', line 5

def options
  @options
end

#prefixObject (readonly)

Returns the value of attribute prefix.



5
6
7
# File 'lib/rails_stuff/statusable/builder.rb', line 5

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



5
6
7
# File 'lib/rails_stuff/statusable/builder.rb', line 5

def suffix
  @suffix
end

Instance Method Details

#each_statusObject

Yields every status with it’s database value into block.



36
37
38
# File 'lib/rails_stuff/statusable/builder.rb', line 36

def each_status
  list.each { |x| yield x, x.to_s }
end

#field_accessorObject



83
84
85
86
# File 'lib/rails_stuff/statusable/builder.rb', line 83

def field_accessor
  field_reader
  field_writer
end

#field_readerObject

Status as symbol.



97
98
99
100
101
102
103
# 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_scopeObject

Scope with given status. Useful for has_scope.



46
47
48
49
# 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_writerObject

Make field accept sympbols.



89
90
91
92
93
94
# 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

#generateObject



16
17
18
19
20
21
22
23
# 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) ⇒ Object

Wraps status name with prefix and suffix.



41
42
43
# File 'lib/rails_stuff/statusable/builder.rb', line 41

def status_method_name(status)
  "#{prefix}#{status}#{suffix}"
end

#translation_helpersObject



105
106
107
108
109
110
111
# 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_listObject

Field reader returns string, so we stringify list for validation.



31
32
33
# File 'lib/rails_stuff/statusable/builder.rb', line 31

def valid_list
  list.map(&:to_s)
end

#validationsObject



25
26
27
28
# 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) ⇒ Object

Generates methods for specific value.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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_accessorsObject

Status accessors for every status.



52
53
54
55
56
# File 'lib/rails_stuff/statusable/builder.rb', line 52

def value_accessors
  each_status do |status, value|
    value_accessor status, value
  end
end

#value_scopesObject

Scopes for every status.



59
60
61
62
63
64
65
# 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