Class: Api::ModelResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/api/model_resolver.rb

Defined Under Namespace

Classes: NotFound

Class Method Summary collapse

Class Method Details

.resolve(params, controller_path, controller_name) ⇒ Object

Resolve model from params, controller_path, or controller_name. Returns nil when no class can be resolved (info/utility controllers use this path). Raises NotFound when a class is resolved but is not an ActiveRecord model (and not TestApi).



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/api/model_resolver.rb', line 8

def self.resolve(params, controller_path, controller_name)
  model = (params[:ctrl].classify.constantize rescue
           params[:path].split("/").first.classify.constantize rescue
           controller_path.classify.constantize rescue
           controller_name.classify.constantize rescue
           nil)
  if model && model != TestApi
    raise NotFound unless (model.new.is_a?(ActiveRecord::Base) rescue false)
  end
  model
end