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.
8
9
10
11
|
# File 'app/models/simply_auth/model.rb', line 8
def initialize(*args)
super(*args)
@persisted = false
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
6
7
8
|
# File 'app/models/simply_auth/model.rb', line 6
def id
@id
end
|
Class Method Details
.collection_path(owner_ids = []) ⇒ Object
89
90
91
92
93
94
95
96
|
# File 'app/models/simply_auth/model.rb', line 89
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
135
136
137
138
139
|
# File 'app/models/simply_auth/model.rb', line 135
def self.create(attributes={})
r = new(attributes)
r.save()
r
end
|
.find(ids) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
|
# File 'app/models/simply_auth/model.rb', line 123
def self.find(ids)
response = RestClient.get(
"https://api.simplyauth.com#{instance_path(ids)}",
accept: :json
)
body = JSON.parse(response.body)
body = body.deep_transform_keys { |key| key.to_s.underscore }
new(body).tap do |r|
r.persisted = true
end
end
|
.instance_path(ids = []) ⇒ Object
114
115
116
117
118
119
120
121
|
# File 'app/models/simply_auth/model.rb', line 114
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
70
71
72
73
74
75
|
# File 'app/models/simply_auth/model.rb', line 70
def self.owner_class
return @owner_class if @owner_class_set
@owner_class = "SimplyAuth::#{self.owner_class_name}".constantize if self.owner_class_name
@owner_class_set = true
@owner_class
end
|
.owner_class_name ⇒ Object
81
82
83
|
# File 'app/models/simply_auth/model.rb', line 81
def self.owner_class_name
nil
end
|
.path_component ⇒ Object
77
78
79
|
# File 'app/models/simply_auth/model.rb', line 77
def self.path_component
model_name.element.pluralize.dasherize
end
|
Instance Method Details
#attributes ⇒ Object
35
36
37
|
# File 'app/models/simply_auth/model.rb', line 35
def attributes
{id: id, data: data.to_h}
end
|
#attributes=(hash) ⇒ Object
29
30
31
32
33
|
# File 'app/models/simply_auth/model.rb', line 29
def attributes=(hash)
hash.each do |key, value|
send("#{key}=", value)
end
end
|
#collection_path ⇒ Object
98
99
100
101
102
103
104
|
# File 'app/models/simply_auth/model.rb', line 98
def collection_path
if owner_id
self.class.collection_path(owner_id)
else
self.class.collection_path
end
end
|
#data ⇒ Object
21
22
23
|
# File 'app/models/simply_auth/model.rb', line 21
def data
@data ||= OpenStruct.new
end
|
#data=(val) ⇒ Object
25
26
27
|
# File 'app/models/simply_auth/model.rb', line 25
def data=(val)
@data = OpenStruct.new(val)
end
|
#instance_path ⇒ Object
106
107
108
109
110
111
112
|
# File 'app/models/simply_auth/model.rb', line 106
def instance_path
if owner_id
self.class.instance_path([owner_id, id])
else
self.class.instance_path(id)
end
end
|
#owner_id ⇒ Object
85
86
87
|
# File 'app/models/simply_auth/model.rb', line 85
def owner_id
nil
end
|
#persisted=(val) ⇒ Object
17
18
19
|
# File 'app/models/simply_auth/model.rb', line 17
def persisted=(val)
@persisted = val
end
|
#persisted? ⇒ Boolean
13
14
15
|
# File 'app/models/simply_auth/model.rb', line 13
def persisted?
@persisted
end
|
#save ⇒ Object
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
|
# File 'app/models/simply_auth/model.rb', line 39
def save
if valid?
Rails.logger.error([
persisted? ? :patch : :post,
"https://api.simplyauth.com#{persisted? ? instance_path : collection_path}",
attributes.to_json,
{accept: :json,
content_type: :json}
].inspect)
response = RestClient.send(
persisted? ? :patch : :post,
"https://api.simplyauth.com#{persisted? ? instance_path : collection_path}",
attributes.to_json,
accept: :json,
content_type: :json
)
Rails.logger.error("3")
body = JSON.parse(response.body)
body = body.deep_transform_keys { |key| key.to_s.underscore }
Rails.logger.error("4")
self.attributes = body
changes_applied
self.persisted = true
Rails.logger.error("5")
true
else
Rails.logger.error("6")
false
end
end
|