Class: Trestle::Resource
- Inherits:
-
Admin
- Object
- Admin
- Trestle::Resource
show all
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/trestle/resource.rb,
lib/trestle/resource/builder.rb,
lib/trestle/resource/collection.rb,
lib/trestle/resource/controller.rb
Defined Under Namespace
Classes: Builder, Collection, Controller
Constant Summary
collapse
- RESOURCE_ACTIONS =
[:index, :show, :new, :create, :edit, :update, :destroy]
- READONLY_ACTIONS =
[:index, :show]
Class Method Summary
collapse
Methods inherited from Admin
admin_name, breadcrumbs, controller_namespace, controller_path, default_breadcrumb, human_admin_name, i18n_key, parameter_name, path, railtie_routes_url_helpers, route_name, table=, tables
Class Method Details
.adapter ⇒ Object
18
19
20
|
# File 'lib/trestle/resource.rb', line 18
def adapter
@adapter ||= Trestle.config.default_adapter.new(self)
end
|
.adapter=(klass) ⇒ Object
22
23
24
|
# File 'lib/trestle/resource.rb', line 22
def adapter=(klass)
@adapter = klass.new(self)
end
|
.adapter_method(name) ⇒ Object
Defines a method that can be overridden with a custom block, but is otherwise delegated to the adapter instance.
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/trestle/resource.rb', line 28
def self.adapter_method(name)
block_method = :"#{name}_block"
attr_accessor block_method
define_method(name) do |*args|
if override = public_send(block_method)
instance_exec(*args, &override)
else
adapter.public_send(name, *args)
end
end
end
|
.build(&block) ⇒ Object
153
154
155
|
# File 'lib/trestle/resource.rb', line 153
def build(&block)
Resource::Builder.build(self, &block)
end
|
.column_sorts ⇒ Object
78
79
80
|
# File 'lib/trestle/resource.rb', line 78
def column_sorts
@column_sorts ||= {}
end
|
86
87
88
|
# File 'lib/trestle/resource.rb', line 86
def form
super || Form::Automatic.new(self)
end
|
.initialize_collection(params) ⇒ Object
70
71
72
|
# File 'lib/trestle/resource.rb', line 70
def initialize_collection(params)
collection(params)
end
|
.instance_path(instance, options = {}) ⇒ Object
123
124
125
126
127
128
|
# File 'lib/trestle/resource.rb', line 123
def instance_path(instance, options={})
action = options.fetch(:action) { :show }
options = options.merge(id: to_param(instance)) unless singular?
path(action, options)
end
|
.model ⇒ Object
90
91
92
|
# File 'lib/trestle/resource.rb', line 90
def model
@model ||= options[:model] || infer_model_class
end
|
.model_name ⇒ Object
94
95
96
|
# File 'lib/trestle/resource.rb', line 94
def model_name
@model_name ||= Trestle::ModelName.new(model)
end
|
.prepare_collection(params) ⇒ Object
66
67
68
|
# File 'lib/trestle/resource.rb', line 66
def prepare_collection(params)
Collection.new(self).prepare(params)
end
|
.readonly? ⇒ Boolean
106
107
108
|
# File 'lib/trestle/resource.rb', line 106
def readonly?
options[:readonly]
end
|
.return_locations ⇒ Object
149
150
151
|
# File 'lib/trestle/resource.rb', line 149
def return_locations
@return_locations ||= {}
end
|
.root_action ⇒ Object
102
103
104
|
# File 'lib/trestle/resource.rb', line 102
def root_action
singular? ? :show : :index
end
|
.routes ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/trestle/resource.rb', line 130
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
instance_exec(&admin.additional_routes) if admin.additional_routes
end
end
end
|
.scopes ⇒ Object
74
75
76
|
# File 'lib/trestle/resource.rb', line 74
def scopes
@scopes ||= {}
end
|
.singular? ⇒ Boolean
110
111
112
|
# File 'lib/trestle/resource.rb', line 110
def singular?
options[:singular]
end
|
.table ⇒ Object
82
83
84
|
# File 'lib/trestle/resource.rb', line 82
def table
super || Table::Automatic.new(self)
end
|
.translate(key, options = {}) ⇒ Object
Also known as:
t
114
115
116
117
118
119
120
|
# File 'lib/trestle/resource.rb', line 114
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
157
158
159
160
161
|
# File 'lib/trestle/resource.rb', line 157
def validate!
if singular? && find_instance_block.nil?
raise NotImplementedError, "Singular resources must define an instance block."
end
end
|