2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-05-08 16:27:49 -04:00
|
|
|
class Topic
|
|
|
|
include ActiveModel::Validations
|
2010-08-20 10:17:29 -04:00
|
|
|
include ActiveModel::Validations::Callbacks
|
2018-08-19 01:57:05 -04:00
|
|
|
include ActiveModel::AttributeMethods
|
|
|
|
include ActiveSupport::NumberHelper
|
|
|
|
|
|
|
|
attribute_method_suffix "_before_type_cast"
|
|
|
|
define_attribute_method :price
|
2010-05-08 16:27:49 -04:00
|
|
|
|
2011-02-05 19:00:57 -05:00
|
|
|
def self._validates_default_keys
|
|
|
|
super | [ :message ]
|
|
|
|
end
|
|
|
|
|
2013-10-23 09:01:14 -04:00
|
|
|
attr_accessor :title, :author_name, :content, :approved, :created_at
|
2010-08-20 10:17:29 -04:00
|
|
|
attr_accessor :after_validation_performed
|
2018-08-19 01:57:05 -04:00
|
|
|
attr_writer :price
|
2010-08-20 10:17:29 -04:00
|
|
|
|
|
|
|
after_validation :perform_after_validation
|
2010-05-08 16:27:49 -04:00
|
|
|
|
|
|
|
def initialize(attributes = {})
|
|
|
|
attributes.each do |key, value|
|
|
|
|
send "#{key}=", value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-20 11:07:49 -04:00
|
|
|
def condition_is_true
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-11-05 17:25:37 -05:00
|
|
|
def condition_is_false
|
2009-03-20 11:07:49 -04:00
|
|
|
false
|
|
|
|
end
|
2010-08-20 10:17:29 -04:00
|
|
|
|
|
|
|
def perform_after_validation
|
|
|
|
self.after_validation_performed = true
|
|
|
|
end
|
|
|
|
|
2011-02-05 19:33:00 -05:00
|
|
|
def my_validation
|
|
|
|
errors.add :title, "is missing" unless title
|
|
|
|
end
|
|
|
|
|
2011-02-05 19:44:35 -05:00
|
|
|
def my_validation_with_arg(attr)
|
|
|
|
errors.add attr, "is missing" unless send(attr)
|
|
|
|
end
|
2018-08-19 01:57:05 -04:00
|
|
|
|
|
|
|
def price
|
|
|
|
number_to_currency @price
|
|
|
|
end
|
|
|
|
|
|
|
|
def attribute_before_type_cast(attr)
|
|
|
|
instance_variable_get(:"@#{attr}")
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|