File: topic.rb

package info (click to toggle)
rails 2%3A7.2.2.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,352 kB
  • sloc: ruby: 349,799; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (64 lines) | stat: -rw-r--r-- 1,223 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

class Topic
  include ActiveModel::Validations
  include ActiveModel::Validations::Callbacks
  include ActiveModel::AttributeMethods
  include ActiveSupport::NumberHelper

  attribute_method_suffix "_before_type_cast", parameters: false
  define_attribute_method :price

  def self._validates_default_keys
    super | [ :message ]
  end

  attr_accessor :title, :author_name, :content, :approved, :created_at
  attr_accessor :after_validation_performed
  attr_writer :price

  after_validation :perform_after_validation

  def initialize(attributes = {})
    attributes.each do |key, value|
      public_send "#{key}=", value
    end
  end

  def condition_is_true
    true
  end

  def condition_is_false
    false
  end

  def perform_after_validation
    self.after_validation_performed = true
  end

  def my_validation
    errors.add :title, "is missing" unless title
  end

  def my_validation_with_arg(attr)
    errors.add attr, "is missing" unless public_send(attr)
  end

  def price
    number_to_currency @price
  end

  def raw_price
    @price
  end

  def attribute_before_type_cast(attr)
    instance_variable_get(:"@#{attr}")
  end

  private
    def five
      5
    end
end