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.3"

Instance Method Summary collapse

Instance Method Details

#collectionObject



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

def collection
  return unless resource_class

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

  return objects
end

#collection_nameObject



32
33
34
# File 'lib/dry_controller.rb', line 32

def collection_name
  controller_name.downcase
end

#resourceObject



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

def resource
  return unless resource_class

  unless obj = instance_variable_get("@#{resource_name}")
    obj ||= if params[:id].present?
      resource_class.where(id: params[:id]).first
    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



36
37
38
39
40
# File 'lib/dry_controller.rb', line 36

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

#resource_missingObject



74
75
76
# File 'lib/dry_controller.rb', line 74

def resource_missing

end

#resource_nameObject



28
29
30
# File 'lib/dry_controller.rb', line 28

def resource_name
  controller_name.singularize.downcase
end