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') .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
|