Module: DomainAssociations

Defined in:
app/models/domain_associations.rb

Class Method Summary collapse

Class Method Details

.when_belongs_to(klass, options) ⇒ Object



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
# File 'app/models/domain_associations.rb', line 3

def self.when_belongs_to(klass, options)
  klass.prefix = "#{RestApi::Base.prefix}domains/:domain_id/"
  klass.class_eval do
    # domain_id overlaps with the attribute returned by the server
    def domain_id=(id)
      if self.prefix_options[:domain_id].nil?
        self.prefix_options[:domain_id] = id
      else
        super
      end
    end
    def domain_id
      super or self.prefix_options[:domain_id]
    end

    def domain
      Domain.find(domain_id, :as => as)
    end

    def domain=(domain)
      self.domain_id = if domain.kind_of?(Domain) 
          self.as = domain.as if self.as.nil?
          domain.id
        else
          domain
        end
    end
  end
end