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

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

.actionsObject


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

def actions
  @actions ||= (readonly? ? READONLY_ACTIONS : RESOURCE_ACTIONS).dup
end

.build(&block) ⇒ Object


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

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

.column_sortsObject


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

def column_sorts
  @column_sorts ||= {}
end

.default_human_admin_nameObject


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

def default_human_admin_name
  model_name.plural
end

.formObject


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

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

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


115
116
117
118
119
120
# File 'lib/trestle/resource.rb', line 115

def instance_path(instance, options={})
  action = options.fetch(:action) { :show }
  options = options.merge(id: to_param(instance)) unless singular?

  path(action, options)
end

.modelObject


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

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

.model_nameObject


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

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

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

Deprecated: use instance method instead


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

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

.readonly?Boolean

Returns:

  • (Boolean)

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

def readonly?
  options[:readonly]
end

.return_locationsObject


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

def return_locations
  @return_locations ||= {}
end

.root_actionObject


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

def root_action
  singular? ? :show : :index
end

.routesObject


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/trestle/resource.rb', line 122

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


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

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

.singular?Boolean

Returns:

  • (Boolean)

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

def singular?
  options[:singular]
end

.tableObject


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

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

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


106
107
108
109
110
111
112
# File 'lib/trestle/resource.rb', line 106

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


151
152
153
154
155
# File 'lib/trestle/resource.rb', line 151

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) and returns a hash of Scope objects keyed by the scope name


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

def scopes
  @scopes ||= self.class.scopes.evaluate(@context)
end