Class: Oauth2::Provider::ModelBase
- Inherits:
-
Object
- Object
- Oauth2::Provider::ModelBase
show all
- Includes:
- Validatable
- Defined in:
- lib/oauth2/provider/model_base.rb
Constant Summary
collapse
- CONVERTORS =
{
:integer => Proc.new { |v| v ? v.to_i : nil },
:string => Proc.new { |v| v.to_s }
}.with_indifferent_access
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ ModelBase
38
39
40
|
# File 'lib/oauth2/provider/model_base.rb', line 38
def initialize(attributes={})
assign_attributes(attributes)
end
|
Class Method Details
.all ⇒ Object
130
131
132
133
134
|
# File 'lib/oauth2/provider/model_base.rb', line 130
def self.all
datasource.send("find_all_#{compact_name}").collect do |dto|
new.update_from_dto(dto)
end
end
|
.columns(*names) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/oauth2/provider/model_base.rb', line 23
def self.columns(*names)
options = names.
names.each do |column_name|
attr_accessor column_name
self.db_columns[column_name.to_s] = CONVERTORS[:string]
end
options.each do |column_name, converter|
attr_accessor column_name
self.db_columns[column_name.to_s] = CONVERTORS[converter]
end
end
|
.compact_name ⇒ Object
144
145
146
|
# File 'lib/oauth2/provider/model_base.rb', line 144
def self.compact_name
self.name.split('::').last.underscore
end
|
.count ⇒ Object
136
137
138
|
# File 'lib/oauth2/provider/model_base.rb', line 136
def self.count
all.size
end
|
.create(attributes = {}) ⇒ Object
148
149
150
151
152
|
# File 'lib/oauth2/provider/model_base.rb', line 148
def self.create(attributes={})
client = self.new(attributes)
client.save
client
end
|
.create!(attributes = {}) ⇒ Object
154
155
156
157
158
|
# File 'lib/oauth2/provider/model_base.rb', line 154
def self.create!(attributes={})
client = self.new(attributes)
client.save!
client
end
|
.datasource ⇒ Object
73
74
75
|
# File 'lib/oauth2/provider/model_base.rb', line 73
def self.datasource
@@datasource ||= default_datasource
end
|
.datasource=(ds) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/oauth2/provider/model_base.rb', line 44
def self.datasource=(ds)
@@datasource = case ds
when NilClass
default_datasource
when String
eval(ds).new
when Class
ds.new
else
ds
end
end
|
.default_datasource ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/oauth2/provider/model_base.rb', line 81
def self.default_datasource
if defined?(ActiveRecord)
ARDatasource.new
else
unless ENV['LOAD_OAUTH_SILENTLY']
puts "*"*80
puts "*** Activerecord is not defined! Using InMemoryDatasource, which will not persist across application restarts!! ***"
puts "*"*80
end
InMemoryDatasource.new
end
end
|
.find(id) ⇒ Object
110
111
112
|
# File 'lib/oauth2/provider/model_base.rb', line 110
def self.find(id)
find_by_id(id) || raise(NotFoundException.new("Record not found!"))
end
|
.find_all_with(column_name, column_value) ⇒ Object
118
119
120
121
122
|
# File 'lib/oauth2/provider/model_base.rb', line 118
def self.find_all_with(column_name, column_value)
datasource.send("find_all_#{compact_name}_by_#{column_name}", convert(column_name, column_value)).collect do |dto|
new.update_from_dto(dto)
end
end
|
.find_by_id(id) ⇒ Object
114
115
116
|
# File 'lib/oauth2/provider/model_base.rb', line 114
def self.find_by_id(id)
find_one(:id, id)
end
|
.find_one(column_name, column_value) ⇒ Object
124
125
126
127
128
|
# File 'lib/oauth2/provider/model_base.rb', line 124
def self.find_one(column_name, column_value)
if dto = datasource.send("find_#{compact_name}_by_#{column_name}", convert(column_name, column_value))
self.new.update_from_dto(dto)
end
end
|
.size ⇒ Object
140
141
142
|
# File 'lib/oauth2/provider/model_base.rb', line 140
def self.size
all.size
end
|
.transaction(&block) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/oauth2/provider/model_base.rb', line 94
def self.transaction(&block)
if datasource.respond_to?(:transaction)
result = nil
datasource.transaction do
result = yield
end
result
else
yield
end
end
|
.validates_uniqueness_of(*columns) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/oauth2/provider/model_base.rb', line 57
def self.validates_uniqueness_of(*columns)
options = columns.
columns.each do |column_name|
self.validates_each column_name, :logic => lambda {
if scope = options[:scope]
dtos = self.class.find_all_with(column_name, self.send(column_name))
dtos = dtos.select{ |o| o.send(scope) == self.send(scope) }
errors.add(column_name, 'is a duplicate.', options[:humanized_name]) if dtos.size == 1 && dtos.first.id != self.id
else
dto = datasource.send("find_#{self.class.compact_name}_by_#{column_name}", read_attribute(column_name))
errors.add(column_name, 'is a duplicate.', options[:humanized_name]) if dto && dto.id != self.id
end
}
end
end
|
Instance Method Details
#assign_attributes(attrs = {}) ⇒ Object
221
222
223
|
# File 'lib/oauth2/provider/model_base.rb', line 221
def assign_attributes(attrs={})
attrs.each { |k, v| write_attribute(k, v) }
end
|
#before_create ⇒ Object
194
195
196
|
# File 'lib/oauth2/provider/model_base.rb', line 194
def before_create
end
|
#before_destroy ⇒ Object
202
203
204
|
# File 'lib/oauth2/provider/model_base.rb', line 202
def before_destroy
end
|
#before_save ⇒ Object
198
199
200
|
# File 'lib/oauth2/provider/model_base.rb', line 198
def before_save
end
|
#datasource ⇒ Object
77
78
79
|
# File 'lib/oauth2/provider/model_base.rb', line 77
def datasource
self.class.datasource
end
|
#destroy ⇒ Object
189
190
191
192
|
# File 'lib/oauth2/provider/model_base.rb', line 189
def destroy
before_destroy
datasource.send("delete_#{self.class.compact_name}", convert(:id, id))
end
|
#new_record? ⇒ Boolean
213
214
215
|
# File 'lib/oauth2/provider/model_base.rb', line 213
def new_record?
id.nil?
end
|
#reload ⇒ Object
185
186
187
|
# File 'lib/oauth2/provider/model_base.rb', line 185
def reload
update_from_dto(self.class.find(id))
end
|
#save ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/oauth2/provider/model_base.rb', line 169
def save
before_create if new_record?
before_save
attrs = db_columns.keys.inject({}) do |result, column_name|
result[column_name] = read_attribute(column_name)
result
end
if self.valid?
dto = datasource.send("save_#{self.class.compact_name}", attrs.with_indifferent_access)
update_from_dto(dto)
return true
end
false
end
|
#save! ⇒ Object
165
166
167
|
# File 'lib/oauth2/provider/model_base.rb', line 165
def save!
save || raise(RecordNotSaved.new("Could not save model!"))
end
|
#to_param ⇒ Object
217
218
219
|
# File 'lib/oauth2/provider/model_base.rb', line 217
def to_param
id.nil? ? nil: id.to_s
end
|
#to_xml(options = {}) ⇒ Object
225
226
227
228
229
230
231
|
# File 'lib/oauth2/provider/model_base.rb', line 225
def to_xml(options = {})
acc = self.db_columns.keys.sort.inject(ActiveSupport::OrderedHash.new) do |acc, key|
acc[key] = self.send(key)
acc
end
acc.to_xml({:root => self.class.name.demodulize.underscore.downcase}.merge(options))
end
|
#transaction(&block) ⇒ Object
106
107
108
|
# File 'lib/oauth2/provider/model_base.rb', line 106
def transaction(&block)
self.class.transaction(&block)
end
|
#update_attributes(attributes = {}) ⇒ Object
160
161
162
163
|
# File 'lib/oauth2/provider/model_base.rb', line 160
def update_attributes(attributes={})
assign_attributes(attributes)
save
end
|
#update_from_dto(dto) ⇒ Object
206
207
208
209
210
211
|
# File 'lib/oauth2/provider/model_base.rb', line 206
def update_from_dto(dto)
db_columns.keys.each do |column_name|
write_attribute(column_name, dto.send(column_name))
end
self
end
|