File: eye.rb

package info (click to toggle)
rails 2%3A7.2.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,348 kB
  • sloc: ruby: 349,797; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (55 lines) | stat: -rw-r--r-- 1,719 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

class Eye < ActiveRecord::Base
  attr_reader :after_create_callbacks_stack
  attr_reader :after_update_callbacks_stack
  attr_reader :after_save_callbacks_stack
  attr_writer :override_iris_with_read_only_foreign_key_color

  # Callbacks configured before the ones has_one sets up.
  after_create :trace_after_create, if: :iris
  after_update :trace_after_update, if: :iris
  after_save   :trace_after_save, if: :iris

  has_one :iris
  accepts_nested_attributes_for :iris

  # Callbacks configured after the ones has_one sets up.
  after_create :trace_after_create2, if: :iris
  after_update :trace_after_update2, if: :iris
  after_save   :trace_after_save2, if: :iris

  def trace_after_create
    (@after_create_callbacks_stack ||= []) << !iris.persisted?
  end
  alias trace_after_create2 trace_after_create

  def trace_after_update
    (@after_update_callbacks_stack ||= []) << iris.has_changes_to_save?
  end
  alias trace_after_update2 trace_after_update

  def trace_after_save
    (@after_save_callbacks_stack ||= []) << iris.has_changes_to_save?
  end
  alias trace_after_save2 trace_after_save

  has_one :iris_with_read_only_foreign_key, class_name: "IrisWithReadOnlyForeignKey", foreign_key: :eye_id
  accepts_nested_attributes_for :iris_with_read_only_foreign_key

  before_save :set_iris_with_read_only_foreign_key_color_to_blue, if: -> {
    iris_with_read_only_foreign_key && @override_iris_with_read_only_foreign_key_color
  }

  def set_iris_with_read_only_foreign_key_color_to_blue
    iris_with_read_only_foreign_key.color = "blue"
  end
end

class Iris < ActiveRecord::Base
  belongs_to :eye
end

class IrisWithReadOnlyForeignKey < Iris
  attr_readonly :eye_id
end