Module: TimestampStates::ClassMethods

Defined in:
lib/timestamp_states.rb

Instance Method Summary collapse

Instance Method Details

#add_timestamp_state_config(column) {|| ... } ⇒ Object

Yields:

  • ()


34
35
36
37
38
# File 'lib/timestamp_states.rb', line 34

def add_timestamp_state_config(column)
  self.timestamp_state_configs ||= {}
  self.timestamp_state_configs[column] ||= {}
  yield(timestamp_state_configs[column])
end

#alias_timestamp_state(alias_name, column, define_scopes: true) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/timestamp_states.rb', line 87

def alias_timestamp_state(alias_name, column, define_scopes: true)
  self.timestamp_state_config_aliases ||= {}
  self.timestamp_state_config_aliases[column] ||= []
  self.timestamp_state_config_aliases[column] << alias_name unless timestamp_state_config_aliases[column].include?(alias_name)

  alias_attribute alias_name, column # ensure the attribute is aliased as well

  timestamp_state(alias_name, define_scopes: define_scopes)
end

#timestamp_state(column, words: {}, define_scopes: true, aliases: []) ⇒ Object

Parameters:

  • column (Symbol)

    The name of the column to define timestamp states for. Example: :published_at, :archived_at, etc

  • options (Hash)

    a customizable set of options



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/timestamp_states.rb', line 46

def timestamp_state(column, words: {}, define_scopes: true, aliases: [])
  add_timestamp_state_config(column) do |config|
    config[:words] = config[:words].to_h.merge(words)
    config[:words][:past] ||= column.to_s.gsub(/_at$/, '').to_sym
    config[:words][:action] ||= { enqueued: :enqueue }[config[:words][:past]]
    config[:words][:action] ||= column.to_s
                                      .sub(/_at$/, '')
                                      .sub(/lled$/, 'll')
                                      .sub(/failed$/, 'fail')
                                      .sub(/ted$/, 't')
                                      .sub(/eeded$/, 'eed')
                                      .sub(/([^tvklure])ed$/, '\1') # spellr:disable-line
                                      .sub(/lle$/, 'l')
                                      .sub(/tte$/, 't')
                                      .sub(/pp$/, 'p').to_sym
    config[:words][:past_not] ||= "not_#{config[:words][:past]}".to_sym
    config[:words][:not_action] ||= "un#{config[:words][:action]}".to_sym

    action_word = config[:words][:action]
    not_action_word = config[:words][:not_action]
    past_word = config[:words][:past]
    past_not_word = config[:words][:past_not]

    if define_scopes
      scope past_word, -> { where.not(column => nil) }
      scope past_not_word, -> { where(column => nil) }
      scope column, ->(range) { with_timestamp_state(column, range) }
    end

    define_model_callbacks action_word, not_action_word

    Array(aliases).each { |alias_name| alias_timestamp_state(alias_name, column, define_scopes: define_scopes) }
  end
end

#timestamp_states(*columns, **options) ⇒ Object



81
82
83
84
85
# File 'lib/timestamp_states.rb', line 81

def timestamp_states(*columns, **options)
  return timestamp_state_configs if columns.empty?

  columns.each { |column| timestamp_state(column, **options) }
end