Class: Trestle::Resource

Inherits:
Admin
  • Object
show all
Extended by:
ActiveSupport::Autoload
Includes:
AdapterMethods
Defined in:
lib/trestle/resource.rb,
lib/trestle/resource/builder.rb,
lib/trestle/resource/collection.rb,
lib/trestle/resource/controller.rb,
lib/trestle/resource/adapter_methods.rb

Defined Under Namespace

Modules: AdapterMethods Classes: Builder, Collection, Controller

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



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

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

.build(&block) ⇒ Object



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

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

.column_sortsObject



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

def column_sorts
  @column_sorts ||= {}
end

.default_human_admin_nameObject



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

def default_human_admin_name
  model_name.plural
end

.formObject



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

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

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



117
118
119
120
121
122
# File 'lib/trestle/resource.rb', line 117

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

  path(action, options)
end

.modelObject



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

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

.model_nameObject



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

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

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

Deprecated: use instance method instead



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

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

.readonly?Boolean

Returns:

  • (Boolean)


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

def readonly?
  options[:readonly]
end

.return_locationsObject



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

def return_locations
  @return_locations ||= {}
end

.root_actionObject



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

def root_action
  singular? ? :show : :index
end

.routesObject



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

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



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

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

.singular?Boolean

Returns:

  • (Boolean)


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

def singular?
  options[:singular]
end

.tableObject



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

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

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



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

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



153
154
155
156
157
# File 'lib/trestle/resource.rb', line 153

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.



49
50
51
# File 'lib/trestle/resource.rb', line 49

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



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

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