Module: DryController

Extended by:
ActiveSupport::Concern
Defined in:
lib/dry_controller.rb,
lib/dry_controller/version.rb,
lib/dry_controller/errors/resource_not_found.rb

Defined Under Namespace

Modules: ClassMethods, Errors

Constant Summary collapse

VERSION =
"0.0.5"

Instance Method Summary collapse

Instance Method Details

#collectionObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/dry_controller.rb', line 65

def collection
  return unless resource_class

  unless objects = instance_variable_get("@#{collection_name}")
    objects = resource_class.scoped
    instance_variable_set("@#{collection_name}", objects)
  end

  return objects
end

#collection_nameObject



34
35
36
# File 'lib/dry_controller.rb', line 34

def collection_name
  controller_name.downcase
end

#resourceObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dry_controller.rb', line 44

def resource
  return unless resource_class

  unless obj = instance_variable_get("@#{resource_name}")
    obj ||= if params[:id].present?
      resource_class.find(params[:id])
    else
      resource_class.new(params[resource_name])
    end

    if obj.blank?
      raise DryController::Errors::ResourceNotFound,
        "#{resource_class.model_name.human}##{params[:id]} could not be found."
    end

    instance_variable_set("@#{resource_name}", obj)
  end

  return obj
end

#resource_classObject



38
39
40
41
42
# File 'lib/dry_controller.rb', line 38

def resource_class
  resource_name.classify.constantize
rescue NameError => e
  return false
end

#resource_missingObject



76
77
78
# File 'lib/dry_controller.rb', line 76

def resource_missing

end

#resource_nameObject



30
31
32
# File 'lib/dry_controller.rb', line 30

def resource_name
  controller_name.singularize.downcase
end