Skip to content

Commit

Permalink
Update Active Record code to Rails 3 onwards
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed May 1, 2012
1 parent 378f63d commit 2eb9186
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
13 changes: 4 additions & 9 deletions lib/orm_adapter/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ class ActiveRecord < Base
# Do not consider these to be part of the class list
def self.except_classes
@@except_classes ||= [
"CGI::Session::ActiveRecordStore::Session",
"ActiveRecord::SessionStore::Session"
]
end

# Gets a list of the available models for this adapter
def self.model_classes
begin
klasses = ::ActiveRecord::Base.__send__(:descendants) # Rails 3
rescue
klasses = ::ActiveRecord::Base.__send__(:subclasses) # Rails 2
end
klasses = ::ActiveRecord::Base.__send__(:descendants)

klasses.select do |klass|
!klass.abstract_class? && !except_classes.include?(klass.name)
Expand All @@ -35,19 +30,19 @@ def get!(id)

# @see OrmAdapter::Base#get
def get(id)
klass.first :conditions => { klass.primary_key => wrap_key(id) }
klass.where(klass.primary_key => wrap_key(id)).first
end

# @see OrmAdapter::Base#find_first
def find_first(options)
conditions, order = extract_conditions_and_order!(options)
klass.first :conditions => conditions_to_fields(conditions), :order => order_clause(order)
klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).first
end

# @see OrmAdapter::Base#find_all
def find_all(options)
conditions, order = extract_conditions_and_order!(options)
klass.all :conditions => conditions_to_fields(conditions), :order => order_clause(order)
klass.where(conditions_to_fields(conditions)).order(*order_clause(order)).all
end

# @see OrmAdapter::Base#create!
Expand Down
2 changes: 1 addition & 1 deletion spec/orm_adapter/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Note < AbstractNoteClass
subject { ActiveRecord::Base::OrmAdapter }

specify "#except_classes should return the names of active record session store classes" do
subject.except_classes.should == ["CGI::Session::ActiveRecordStore::Session", "ActiveRecord::SessionStore::Session"]
subject.except_classes.should == ["ActiveRecord::SessionStore::Session"]
end

specify "#model_classes should return all of the non abstract model classes (that are not in except_classes)" do
Expand Down

0 comments on commit 2eb9186

Please sign in to comment.