Skip to content

Instantly share code, notes, and snippets.

@xdougx
Last active January 21, 2016 12:02
Show Gist options
  • Save xdougx/04def5e00dfc1efe02f6 to your computer and use it in GitHub Desktop.
Save xdougx/04def5e00dfc1efe02f6 to your computer and use it in GitHub Desktop.
# Depende da gem exceptions-resource, só procurar no meu git que você encontra
module Statusable
extend ActiveSupport::Concern
included do
available_statuses.each do |status|
scope status, -> { where(status: status) }
end
end
def status?(status)
status.delete!('?')
self.status == status
end
def change_status(status)
if self.class.available_statuses.include? status
update_attribute(:status, status)
else
fail I18n.t('exceptions.status.not_found')
end
end
# vevify if is active, if not raise an exception
def is_active?
self.class.raise_model(:login, 'exceptions.auth.inactive') unless self.active?
true
end
def is_status?(status)
if status.last == '?'
status = status.delete('?')
self.class.available_statuses.include? status
end
end
def is_change_status?(status)
if status.last == '!'
status = status.delete('!')
self.class.available_statuses.include? status
end
end
def method_missing(method, *args, &block)
method_string = method.to_s
if is_status? method_string
self.status? method_string
elsif is_change_status? method_string
change_status(method_string.delete('!'))
else
super
end
end
def respond_to?(method_sym, include_private = false)
if is_status?(method_sym.to_s) || is_change_status?(method_sym.to_s)
true
else
super
end
end
module ClassMethods
def available_statuses
%w(active archived inative destroyed temporary) +
%w(waiting aproved reproved answered closed)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment