Class: SimplyAuth::Model
- Inherits:
-
Object
show all
- Includes:
- ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Model
- Defined in:
- app/models/simply_auth/model.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ Model
Returns a new instance of Model.
10
11
12
13
|
# File 'app/models/simply_auth/model.rb', line 10
def initialize(*args)
super(*args)
@persisted = false
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
8
9
10
|
# File 'app/models/simply_auth/model.rb', line 8
def id
@id
end
|
Class Method Details
.collection_path(owner_ids = []) ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'app/models/simply_auth/model.rb', line 98
def self.collection_path(owner_ids = [])
ids = [ids].flatten
if self.owner_class
"#{self.owner_class.instance_path(owner_ids)}/#{path_component}"
else
"/#{path_component}"
end
end
|
.create(attributes = {}) ⇒ Object
144
145
146
147
148
|
# File 'app/models/simply_auth/model.rb', line 144
def self.create(attributes={})
r = new(attributes)
r.save()
r
end
|
.find(ids) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
|
# File 'app/models/simply_auth/model.rb', line 132
def self.find(ids)
response = RestClient.get(
"https://api.simplyauth.com#{instance_path(ids)}",
accept: :json
)
body = JSON.parse(response.body)[model_name.element.camelize(:lower)]
body = body.deep_transform_keys { |key| key.to_s.underscore }
new(body).tap do |r|
r.persisted = true
end
end
|
.instance_path(ids = []) ⇒ Object
123
124
125
126
127
128
129
130
|
# File 'app/models/simply_auth/model.rb', line 123
def self.instance_path(ids = [])
ids = [ids].flatten
if self.owner_class
"#{self.owner_class.instance_path(ids.first(ids.length - 1))}/#{path_component}/#{ERB::Util.url_encode(ids.last)}"
else
"/#{path_component}/#{ERB::Util.url_encode(ids.last)}"
end
end
|
.owner_class ⇒ Object
77
78
79
80
81
82
83
84
|
# File 'app/models/simply_auth/model.rb', line 77
def self.owner_class
@owner_class_set ||= false
return @owner_class if @owner_class_set
return nil unless self.owner_class_name
@owner_class = "SimplyAuth::#{self.owner_class_name}".constantize
@owner_class_set = true
@owner_class
end
|
.owner_class_name ⇒ Object
90
91
92
|
# File 'app/models/simply_auth/model.rb', line 90
def self.owner_class_name
nil
end
|
.path_component ⇒ Object
86
87
88
|
# File 'app/models/simply_auth/model.rb', line 86
def self.path_component
model_name.element.pluralize.dasherize
end
|
Instance Method Details
#attributes ⇒ Object
37
38
39
|
# File 'app/models/simply_auth/model.rb', line 37
def attributes
{id: id, data: data.to_h}
end
|
#attributes=(hash) ⇒ Object
31
32
33
34
35
|
# File 'app/models/simply_auth/model.rb', line 31
def attributes=(hash)
hash.each do |key, value|
send("#{key}=", value)
end
end
|
#collection_path ⇒ Object
107
108
109
110
111
112
113
|
# File 'app/models/simply_auth/model.rb', line 107
def collection_path
if owner_id
self.class.collection_path(owner_id)
else
self.class.collection_path
end
end
|
#data ⇒ Object
23
24
25
|
# File 'app/models/simply_auth/model.rb', line 23
def data
@data ||= OpenStruct.new
end
|
#data=(val) ⇒ Object
27
28
29
|
# File 'app/models/simply_auth/model.rb', line 27
def data=(val)
@data = OpenStruct.new(val)
end
|
#instance_path ⇒ Object
115
116
117
118
119
120
121
|
# File 'app/models/simply_auth/model.rb', line 115
def instance_path
if owner_id
self.class.instance_path([owner_id, id])
else
self.class.instance_path(id)
end
end
|
#owner_id ⇒ Object
94
95
96
|
# File 'app/models/simply_auth/model.rb', line 94
def owner_id
nil
end
|
#persisted=(val) ⇒ Object
19
20
21
|
# File 'app/models/simply_auth/model.rb', line 19
def persisted=(val)
@persisted = val
end
|
#persisted? ⇒ Boolean
15
16
17
|
# File 'app/models/simply_auth/model.rb', line 15
def persisted?
@persisted
end
|
#save ⇒ Object
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
|
# File 'app/models/simply_auth/model.rb', line 41
def save
if valid?
attributes = self.attributes.deep_transform_keys{|k| k.to_s.camelize(:lower)}
response = RestClient.send(
persisted? ? :patch : :post,
"https://api.simplyauth.com#{persisted? ? instance_path : collection_path}",
{model_name.element.camelize(:lower) => attributes}.to_json,
accept: :json,
content_type: :json
)
body = JSON.parse(response.body)[model_name.element.camelize(:lower)]
body = body.deep_transform_keys { |key| key.to_s.underscore } if body
self.attributes = self.class.new.attributes.merge(body || {})
changes_applied
self.persisted = true
true
else
false
end
rescue RestClient::UnprocessableEntity => e
errors = JSON.parse(e.http_body)["errors"]
errors.each do |k, v|
v.each do |err|
self.errors.add(k, err)
end
end
false
end
|