Module: RecordWithOperator

Defined in:
lib/record_with_operator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



136
137
138
139
140
141
142
143
144
145
# File 'lib/record_with_operator.rb', line 136

def method_missing(method, *args)
  return super unless respond_to?(method)
  case method.to_sym
  when :creator, :updater, :deleter
    self.class.create_operator_associations
    send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#operatorObject

Returns the value of attribute operator.



7
8
9
# File 'lib/record_with_operator.rb', line 7

def operator
  @operator
end

Class Method Details

.configObject



2
3
4
5
# File 'lib/record_with_operator.rb', line 2

def self.config
  @config ||= {:user_class_name => "User", :operator_association_options => {}}
  @config
end

.included(base) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/record_with_operator.rb', line 9

def self.included(base)
  class << base
    def reflections_with_operator
      create_operator_associations
      reflections_without_operator
    end
    alias_method_chain :reflections, :operator

    def operator_associations_created?
      @operator_associations_created
    end

    def create_operator_associations
      return if operator_associations_created?
      @operator_associations_created = true

      if self.table_exists?
        belongs_to :creator, {:foreign_key => "created_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('created_by')
        belongs_to :updater, {:foreign_key => "updated_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('updated_by')
        belongs_to :deleter, {:foreign_key => "deleted_by", :class_name => RecordWithOperator.config[:user_class_name]}.merge(RecordWithOperator.config[:operator_association_options]) if column_names.include?('deleted_by')
      end
    end

    def has_many_with_operator(*args, &extension)
      options = args.extract_options!
      # add AssociationWithOprator to :extend
      if options[:extend]
        options[:extend] = [options[:extend]] unless options[:extend].kind_of? Array
        options[:extend] << AssociationWithOperator
      else
        options[:extend] = AssociationWithOperator
      end
      # add :set_operator to :before_add
      if options[:before_add]
        options[:before_add] = [options[:before_add]] unless options[:before_add].kind_of? Array
        options[:before_add] << :set_operator
      else
        options[:before_add] = :set_operator
      end
      args << options
      has_many_without_operator(*args, &extension)
    end
    alias_method_chain :has_many, :operator

    def find_with_for(*args)
      options = args.extract_options!
      operator = options.delete(:for)
      args << options
      results = find_without_for(*args)
      if operator
        if results.kind_of? Array
          results.each{|r| r.operator = operator}
        elsif results
          results.operator = operator
        end
      end
      results
    end

    alias_method_chain :find, :for

    def validate_find_options_with_for(options)
      if options
        options = options.dup
        options.delete(:for)
      end
      validate_find_options_without_for(options)
    end

    alias_method_chain :validate_find_options, :for

    private
    # define_method association, association= ...
    def association_accessor_methods_with_operator(reflection, association_proxy_class)
      association_accessor_methods_without_operator(reflection, association_proxy_class)
      define_method("#{reflection.name}_with_operator") do |*params|
        r = send("#{reflection.name}_without_operator", *params)
        r.operator ||= self.operator if r && r.respond_to?(:operator=)
        r
      end
      alias_method_chain "#{reflection.name}".to_sym, :operator

      define_method("#{reflection.name}_with_operator=") do |new_value|
        new_value.operator ||= self.operator if new_value.respond_to?(:operator=)
        send("#{reflection.name}_without_operator=", new_value)
      end
      alias_method_chain "#{reflection.name}=".to_sym, :operator
    end
    alias_method_chain :association_accessor_methods, :operator

    # define_method build_association, create_association ...
    def association_constructor_method_with_operator(constructor, reflection, association_proxy_class)
      association_constructor_method_without_operator(constructor, reflection, association_proxy_class)
      define_method("#{constructor}_#{reflection.name}_with_operator") do |*params|
        options = { :operator => self.operator }
        params.empty? ? params[0] = options : params.first.merge!(options)
        self.send("#{constructor}_#{reflection.name}_without_operator", *params)
      end
      alias_method_chain "#{constructor}_#{reflection.name}".to_sym, :operator
    end
    alias_method_chain :association_constructor_method, :operator
  end

  base.before_create :set_created_by
  base.before_save :set_updated_by
  base.before_destroy :set_deleted_by

end

Instance Method Details

#respond_to?(name, priv = false) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/record_with_operator.rb', line 118

def respond_to?(name, priv=false)
  case name.to_sym
  when :creator
    respond_to? :created_by
  when :updater
    respond_to? :updated_by
  when :deleter
    respond_to? :deleted_by
  else
    super
  end
end