Class: Mailroute::Base
Direct Known Subclasses
Admin, BrandingInfo, Customer, CustomerContact, Domain, DomainAlias, DomainContact, EmailAccount, EmailAccountContact, LocalpartAlias, MailServer, NotificationAccountTask, NotificationDomainTask, OutboundServer, PolicyDomain, PolicyUser, Reseller, ResellerContact, WBList
Class Method Summary
collapse
Instance Method Summary
collapse
included
Constructor Details
#initialize(attributes = {}, persisted = false) ⇒ Base
Returns a new instance of Base.
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/mailroute/models/base.rb', line 155
def initialize(attributes = {}, persisted = false)
@_associations = {}.with_indifferent_access
new_attributes = attributes.dup
attributes.each do |k, v|
next unless self.class.meta.associations.has_key?(k.to_sym)
relation = self.class.meta.associations[k.to_sym]
case v
when Integer
new_attributes[k] = relation.foreign_class.element_path(v)
when Mailroute::Base
@_associations[k] = v
new_attributes[k] = v.element_path
when String
if relation.pk && v !~ /^\/api\/v1/
all = relation.foreign_class.filter(relation.pk => v).limit(2).to_a
case all.size
when 0 then raise 'there is no such records'
when 2 then raise 'there are too many records'
end
fst = all.first
@_associations[k] = fst
new_attributes[k] = fst.element_path
else
new_attributes[k] = v
end
end
end
super(new_attributes, persisted)
end
|
Class Method Details
.bulk_create(*attribute_array) ⇒ Object
26
27
28
29
30
|
# File 'lib/mailroute/models/base.rb', line 26
def bulk_create(*attribute_array)
attribute_array.map do |attributes|
create(attributes)
end
end
|
.delete(*array) ⇒ Object
34
35
36
37
38
39
40
41
42
|
# File 'lib/mailroute/models/base.rb', line 34
def delete(*array)
array.map do |r|
if r.is_a? Mailroute::Base
r.destroy
else
connection.delete(element_path(r, {}), )
end
end
end
|
.has_admins(options = {}) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/mailroute/models/base.rb', line 117
def self.has_admins(options = {})
relation = meta.add_has_admins(options)
self.send(:define_method, :admins) do
foreign_class = relation.foreign_class
@_associations[:admins] ||= foreign_class.all(:params => {:scope => { :name => relation.inverse.to_s, :id => id}}).to_a
end
self.send(:define_method, :create_admin) do |email, send_welcome|
send_welcome = !!send_welcome
foreign_class = relation.foreign_class
@_associations[:admin] = nil
admin = relation.foreign_class.new(:email => email, :send_welcome => send_welcome)
admin.prefix_options[:scope] = { :name => relation.inverse.to_s, :id => id }
admin.save!
admin
end
self.send(:define_method, :delete_admin) do |email|
foreign_class = relation.foreign_class
@_associations[:admin] = nil
admin = relation.foreign_class.all(:params => {:email => email, :scope => { :name => relation.inverse.to_s, :id => id}}).first
if admin
admin.prefix_options[:scope] = { :name => relation.inverse.to_s, :id => id }
admin.destroy
end
end
end
|
.has_many(model, options = {}) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/mailroute/models/base.rb', line 96
def self.has_many(model, options = {})
relation = meta.add_has_many(model, options)
self.send(:define_method, model.to_s) do
foreign_class = relation.foreign_class
@_associations[model] ||= foreign_class.filter(relation.inverse.to_sym => id).to_a
end
self.send(:define_method, "create_#{ActiveSupport::Inflector.singularize(model.to_s)}") do |options|
foreign_class = relation.foreign_class
@_associations[model] = nil
if relation.pk && options.is_a?(String) && options !~ /^\/api\/v1/
relation.foreign_class.create(relation.inverse.to_s => id, relation.pk => options)
else
relation.foreign_class.create(options.merge(relation.inverse.to_s => id))
end
end
end
|
.has_one(model, options = {}) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/mailroute/models/base.rb', line 62
def self.has_one(model, options = {})
relation = meta.add_has_one(model, options)
self.send(:define_method, model.to_s) do
foreign_class = relation.foreign_class
@_associations[model] ||= foreign_class.get((super())).tap do |obj|
obj.send("#{relation.inverse}=", self)
end if super()
end
self.send(:define_method, "#{model}=") do |v|
if v.is_a? Mailroute::Base
@_associations[model] = v
super(v.resource_uri)
else
super(v)
end
end
end
|
10
11
12
13
14
15
16
17
18
|
# File 'lib/mailroute/models/base.rb', line 10
def
if defined?(@headers)
@headers
elsif superclass.respond_to? :headers
@headers ||= superclass.
else
@headers ||= {}
end
end
|
.instantiate_collection(collection, prefix_options = {}) ⇒ Object
45
46
47
48
49
|
# File 'lib/mailroute/models/base.rb', line 45
def instantiate_collection(collection, prefix_options = {})
array = instantiate_collection_without_meta(collection, prefix_options)
array.instance_variable_set(:@_meta, collection.instance_variable_get(:@_meta))
array
end
|
44
|
# File 'lib/mailroute/models/base.rb', line 44
alias_method :instantiate_collection_without_meta, :instantiate_collection
|
.list(options = {}) ⇒ Object
22
23
24
|
# File 'lib/mailroute/models/base.rb', line 22
def list(options = {})
Relation.new(self)
end
|
6
7
8
|
# File 'lib/mailroute/models/base.rb', line 6
def meta
@meta ||= MetaInformation.new(self)
end
|
.total_count ⇒ Object
51
52
53
|
# File 'lib/mailroute/models/base.rb', line 51
def total_count
limit(1).total_count
end
|
Instance Method Details
#load(attributes, remove_root = false) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/mailroute/models/base.rb', line 84
def load(attributes, remove_root = false)
new_attributes = {}
attributes.each do |k, v|
new_attributes[k] = if v.is_a? Mailroute::Base
v.resource_uri
else
v
end
end
super(new_attributes, remove_root)
end
|
150
151
152
153
|
# File 'lib/mailroute/models/base.rb', line 150
def reload
@_associations = {}.with_indifferent_access
super
end
|
#to_json(options = {}) ⇒ Object
56
57
58
|
# File 'lib/mailroute/models/base.rb', line 56
def to_json(options = {})
super(options.merge(:root => false))
end
|