Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
unify as_id instance and class methods
model_class.as_id is expected to return nil if it cannot compute the id for the given model
  • Loading branch information
ninjudd committed Mar 14, 2013
1 parent 3032a68 commit 7ef69a3
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions lib/model_set.rb
Expand Up @@ -483,14 +483,14 @@ def self.as_ids(models)
end

def self.as_id(model)
if model_class.respond_to?(:as_id)
model_class.as_id(model)
else
case model
when ActiveRecord::Base then model.id
when String then model.split('-').last.to_i
else model.to_i
end
id = model_class.as_id(model) if model_class.respond_to?(:as_id)
return id if id

case model
when model_class then model.send(id_field)
when ActiveRecord::Base then model.id
when String then model.split('-').last.to_i
else model.to_i
end
end

Expand Down Expand Up @@ -650,15 +650,10 @@ def after_fetch(model)
end

def as_id(model)
case model
when model_class
id = self.class.as_id(model)
if model.kind_of?(model_class)
# Save the model object if it is of the same type as our models.
id = model.send(id_field)
models_by_id[id] ||= model
when ActiveRecord::Base
id = model.id
else
id = model.to_i
end
raise "id not found for model: #{model.inspect}" if id.nil?
id
Expand Down

0 comments on commit 7ef69a3

Please sign in to comment.