class Trestle::Resource

Constants

READONLY_ACTIONS
RESOURCE_ACTIONS

Public Class Methods

actions() click to toggle source
# File lib/trestle/resource.rb, line 91
def actions
  @actions ||= (readonly? ? READONLY_ACTIONS : RESOURCE_ACTIONS).dup
end
build(&block) click to toggle source
# File lib/trestle/resource.rb, line 148
def build(&block)
  Resource::Builder.build(self, &block)
end
column_sorts() click to toggle source
# File lib/trestle/resource.rb, line 67
def column_sorts
  @column_sorts ||= {}
end
default_human_admin_name() click to toggle source
# File lib/trestle/resource.rb, line 87
def default_human_admin_name
  model_name.plural
end
form() click to toggle source
Calls superclass method
# File lib/trestle/resource.rb, line 75
def form
  super || Form::Automatic.new(self)
end
instance_path(instance, options={}) click to toggle source
# File lib/trestle/resource.rb, line 116
def instance_path(instance, options={})
  action = options.fetch(:action) { :show }
  options = options.merge(id: to_param(instance)) unless singular?

  path(action, options)
end
model() click to toggle source
# File lib/trestle/resource.rb, line 79
def model
  @model ||= options[:model] || infer_model_class
end
model_name() click to toggle source
# File lib/trestle/resource.rb, line 83
def model_name
  @model_name ||= Trestle::ModelName.new(model)
end
prepare_collection(params, options={}) click to toggle source

Deprecated: use instance method instead

# File lib/trestle/resource.rb, line 59
def prepare_collection(params, options={})
  Collection.new(self, options).prepare(params)
end
readonly?() click to toggle source
# File lib/trestle/resource.rb, line 99
def readonly?
  options[:readonly]
end
return_locations() click to toggle source
# File lib/trestle/resource.rb, line 144
def return_locations
  @return_locations ||= {}
end
root_action() click to toggle source
# File lib/trestle/resource.rb, line 95
def root_action
  singular? ? :show : :index
end
routes() click to toggle source
# File lib/trestle/resource.rb, line 123
def routes
  admin = self

  resource_method  = singular? ? :resource : :resources
  resource_name    = admin_name
  resource_options = {
    controller: controller_namespace,
    as:         route_name,
    path:       options[:path],
    except:     (RESOURCE_ACTIONS - actions)
  }

  Proc.new do
    public_send(resource_method, resource_name, resource_options) do
      admin.additional_routes.each do |block|
        instance_exec(&block)
      end
    end
  end
end
scopes() click to toggle source
# File lib/trestle/resource.rb, line 63
def scopes
  @scopes ||= Scopes::Definition.new
end
singular?() click to toggle source
# File lib/trestle/resource.rb, line 103
def singular?
  options[:singular]
end
t(key, options={})
Alias for: translate
table() click to toggle source
Calls superclass method Trestle::Admin::table
# File lib/trestle/resource.rb, line 71
def table
  super || Table::Automatic.new(self)
end
translate(key, options={}) click to toggle source
Calls superclass method Trestle::Admin::translate
# File lib/trestle/resource.rb, line 107
def translate(key, options={})
  super(key, options.merge({
    model_name:            model_name.titleize,
    lowercase_model_name:  model_name.downcase,
    pluralized_model_name: model_name.plural.titleize
  }))
end
Also aliased as: t
validate!() click to toggle source
# File lib/trestle/resource.rb, line 152
def validate!
  if singular? && !adapter_methods.method_defined?(:find_instance)
    raise NotImplementedError, "Singular resources must define an instance block."
  end
end

Private Class Methods

infer_model_class() click to toggle source
# File lib/trestle/resource.rb, line 159
def infer_model_class
  scope = respond_to?(:module_parent) ? module_parent : parent
  scope.const_get(admin_name.classify)
rescue NameError
  raise NameError, "Unable to find model #{admin_name.classify}. Specify a different model using Trestle.resource(:#{admin_name}, model: MyModel)"
end

Public Instance Methods

prepare_collection(params, options={}) click to toggle source

Prepares a collection for use in the resource controller's index action.

Applies scopes, sorts, pagination, finalization and decorators according to the admin's adapter and any admin-specific adapter methods.

# File lib/trestle/resource.rb, line 47
def prepare_collection(params, options={})
  Collection.new(self, options).prepare(params)
end
scopes() click to toggle source

Evaluates the admin's scope block(s) using the adapter context and returns a hash of Scope objects keyed by the scope name.

# File lib/trestle/resource.rb, line 53
def scopes
  @scopes ||= Scopes.new(self.class.scopes, adapter)
end