Class: Trestle::Resource

Inherits:
Admin
  • Object
show all
Includes:
AdapterMethods
Defined in:
lib/trestle/resource.rb,
lib/trestle/resource/builder.rb,
lib/trestle/resource/toolbar.rb,
lib/trestle/resource/collection.rb,
lib/trestle/resource/adapter_methods.rb,
app/controllers/concerns/trestle/resource/controller/actions.rb,
app/controllers/concerns/trestle/resource/controller/toolbar.rb,
app/controllers/concerns/trestle/resource/controller/redirection.rb,
app/controllers/concerns/trestle/resource/controller/data_methods.rb

Defined Under Namespace

Modules: AdapterMethods, Controller, Toolbar Classes: Builder, Collection

Constant Summary collapse

RESOURCE_ACTIONS =
[:index, :show, :new, :create, :edit, :update, :destroy]
READONLY_ACTIONS =
[:index, :show]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AdapterMethods

#adapter

Methods inherited from Admin

actions, additional_routes, admin_name, breadcrumbs, controller_namespace, default_breadcrumb, default_view_path, hooks, human_admin_name, i18n_key, #initialize, #method_missing, parameter_name, path, railtie_routes_url_helpers, #respond_to_missing?, route_name, table=, tables, to_param, view_path_prefixes

Constructor Details

This class inherits a constructor from Trestle::Admin

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Trestle::Admin

Class Method Details

.build(&block) ⇒ Object



148
149
150
# File 'lib/trestle/resource.rb', line 148

def build(&block)
  Resource::Builder.build(self, &block)
end

.column_sortsObject



67
68
69
# File 'lib/trestle/resource.rb', line 67

def column_sorts
  @column_sorts ||= {}
end

.default_actionsObject



91
92
93
# File 'lib/trestle/resource.rb', line 91

def default_actions
  readonly? ? READONLY_ACTIONS : RESOURCE_ACTIONS
end

.default_human_admin_nameObject



87
88
89
# File 'lib/trestle/resource.rb', line 87

def default_human_admin_name
  model_name.plural
end

.formObject



75
76
77
# File 'lib/trestle/resource.rb', line 75

def form
  super || Form::Automatic.new(self)
end

.instance_path(instance, options = {}) ⇒ Object



116
117
118
119
120
121
# 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

.modelObject



79
80
81
# File 'lib/trestle/resource.rb', line 79

def model
  @model ||= options[:model] || infer_model_class
end

.model_nameObject



83
84
85
# File 'lib/trestle/resource.rb', line 83

def model_name
  @model_name ||= Trestle::ModelName.new(model)
end

.prepare_collection(params, options = {}) ⇒ Object

Deprecated: use instance method instead



59
60
61
# File 'lib/trestle/resource.rb', line 59

def prepare_collection(params, options={})
  Collection.new(self, options).prepare(params)
end

.readonly?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/trestle/resource.rb', line 99

def readonly?
  options[:readonly]
end

.return_locationsObject



144
145
146
# File 'lib/trestle/resource.rb', line 144

def return_locations
  @return_locations ||= {}
end

.root_actionObject



95
96
97
# File 'lib/trestle/resource.rb', line 95

def root_action
  singular? ? :show : :index
end

.routesObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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

.scopesObject



63
64
65
# File 'lib/trestle/resource.rb', line 63

def scopes
  @scopes ||= Scopes::Definition.new
end

.singular?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/trestle/resource.rb', line 103

def singular?
  options[:singular]
end

.tableObject



71
72
73
# File 'lib/trestle/resource.rb', line 71

def table
  super || Table::Automatic.new(self)
end

.translate(key, options = {}) ⇒ Object Also known as: t



107
108
109
110
111
112
113
# 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

.validate!Object



152
153
154
155
156
# 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

Instance Method Details

#prepare_collection(params, options = {}) ⇒ Object

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.



47
48
49
# File 'lib/trestle/resource.rb', line 47

def prepare_collection(params, options={})
  Collection.new(self, options).prepare(params)
end

#scopesObject

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



53
54
55
# File 'lib/trestle/resource.rb', line 53

def scopes
  @scopes ||= Scopes.new(self.class.scopes, adapter)
end