Class: Devise::Activeresource::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/devise/activeresource/adapter.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Adapter

Returns a new instance of Adapter.



8
9
10
# File 'lib/devise/activeresource/adapter.rb', line 8

def initialize(klass)
  @klass = klass
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/devise/activeresource/adapter.rb', line 6

def klass
  @klass
end

Instance Method Details

#column_namesObject

Get a list of column/property/field names



13
14
15
# File 'lib/devise/activeresource/adapter.rb', line 13

def column_names
  klass.attributes
end

#create!(attributes = {}) ⇒ Object

Create a model using attributes



62
63
64
# File 'lib/devise/activeresource/adapter.rb', line 62

def create!(attributes = {})
  klass.create(attributes)
end

#destroy(object) ⇒ Object

Destroy an instance by passing in the instance itself.



67
68
69
# File 'lib/devise/activeresource/adapter.rb', line 67

def destroy(object)
  object.destroy
end

#find_all(options = {}) ⇒ Object

Find all models, optionally matching conditions, and specifying order

See Also:

  • for how to specify order and conditions


55
56
57
58
59
# File 'lib/devise/activeresource/adapter.rb', line 55

def find_all(options = {})
  conditions, limit, offset = extract_conditions!(options)
  params = conditions.merge(limit_offset_hash(limit, offset))
  klass.find(:all, params: params)
end

#find_first(options = {}) ⇒ Object

Find the first instance, optionally matching conditions

You can call with just conditions, providing a hash

User.to_adapter.find_first :name => "Fred", :age => 23

User.to_adapter.find_first :conditions => {:name => "Fred", :age => 23}


45
46
47
48
49
50
51
# File 'lib/devise/activeresource/adapter.rb', line 45

def find_first(options = {})
  conditions, limit, offset = extract_conditions!(options)
  params = conditions.merge(limit_offset_hash(limit, offset))
  klass.find(:first, params: params)
rescue ActiveResource::InvalidRequestError
  nil
end

#get(id) ⇒ Object

Get an instance by id of the model. Returns nil if a model is not found. This should comply with ActiveModel#to_key API, i.e.:

User.to_adapter.get(@user.to_key) == @user


31
32
33
34
35
# File 'lib/devise/activeresource/adapter.rb', line 31

def get(id)
  klass.find(wrap_key(id))
rescue ActiveResource::ResourceNotFound
  nil
end

#get!(id) ⇒ Object

Get an instance by id of the model. Raises an error if a model is not found. This should comply with ActiveModel#to_key API, i.e.:

User.to_adapter.get!(@user.to_key) == @user


22
23
24
# File 'lib/devise/activeresource/adapter.rb', line 22

def get!(id)
  klass.find(wrap_key(id))
end