Class: Trestle::Admin

Inherits:
Object
  • Object
show all
Defined in:
lib/trestle/admin.rb,
lib/trestle/admin/builder.rb

Direct Known Subclasses

Resource

Defined Under Namespace

Classes: Builder

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil) ⇒ Admin

Returns a new instance of Admin.


7
8
9
# File 'lib/trestle/admin.rb', line 7

def initialize(context=nil)
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegate all missing methods to corresponding class method if available


12
13
14
15
16
17
18
# File 'lib/trestle/admin.rb', line 12

def method_missing(name, *args, &block)
  if self.class.respond_to?(name)
    self.class.send(name, *args, &block)
  else
    super
  end
end

Class Attribute Details


58
59
60
61
62
63
64
# File 'lib/trestle/admin.rb', line 58

def breadcrumb
  if @breadcrumb
    Breadcrumb.cast(@breadcrumb.call)
  else
    default_breadcrumb
  end
end

.formObject

Returns the value of attribute form


27
28
29
# File 'lib/trestle/admin.rb', line 27

def form
  @form
end

Returns the value of attribute menu


25
26
27
# File 'lib/trestle/admin.rb', line 25

def menu
  @menu
end

.optionsObject


32
33
34
# File 'lib/trestle/admin.rb', line 32

def options
  @options ||= {}
end

.view_pathObject


107
108
109
# File 'lib/trestle/admin.rb', line 107

def view_path
  @view_path || default_view_path
end

Class Method Details

.actionsObject


131
132
133
# File 'lib/trestle/admin.rb', line 131

def actions
  [:index]
end

.additional_routesObject


139
140
141
# File 'lib/trestle/admin.rb', line 139

def additional_routes
  @additional_routes ||= []
end

.admin_nameObject


73
74
75
# File 'lib/trestle/admin.rb', line 73

def admin_name
  name.underscore.sub(/_admin$/, '')
end

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

def breadcrumbs
  Breadcrumb::Trail.new(Array(Trestle.config.root_breadcrumbs) + [breadcrumb])
end

.build(&block) ⇒ Object


161
162
163
# File 'lib/trestle/admin.rb', line 161

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

.controller_namespaceObject


119
120
121
# File 'lib/trestle/admin.rb', line 119

def controller_namespace
  "#{name.underscore}/admin"
end

.default_breadcrumbObject


66
67
68
69
70
71
# File 'lib/trestle/admin.rb', line 66

def default_breadcrumb
  deprecated = I18n.t(:"admin.breadcrumbs.#{i18n_key}", default: human_admin_name)
  label = translate("breadcrumbs.index", default: deprecated)

  Breadcrumb.new(label, path)
end

.default_human_admin_nameObject


85
86
87
# File 'lib/trestle/admin.rb', line 85

def default_human_admin_name
  name.demodulize.underscore.sub(/_admin$/, '').titleize
end

.default_view_pathObject


111
112
113
# File 'lib/trestle/admin.rb', line 111

def default_view_path
  "admin/#{name.underscore.sub(/_admin$/, '')}"
end

.hooksObject


40
41
42
# File 'lib/trestle/admin.rb', line 40

def hooks
  @hooks ||= Hook::Set.new
end

.human_admin_nameObject


81
82
83
# File 'lib/trestle/admin.rb', line 81

def human_admin_name
  translate("name", default: default_human_admin_name)
end

.i18n_keyObject


77
78
79
# File 'lib/trestle/admin.rb', line 77

def i18n_key
  admin_name
end

.parameter_nameObject


97
98
99
# File 'lib/trestle/admin.rb', line 97

def parameter_name
  admin_name.singularize
end

.path(action = root_action, options = {}) ⇒ Object


123
124
125
# File 'lib/trestle/admin.rb', line 123

def path(action=root_action, options={})
  Engine.routes.url_for(options.merge(controller: controller_namespace, action: action, only_path: true))
end

.railtie_routes_url_helpers(include_path_helpers = true) ⇒ Object


157
158
159
# File 'lib/trestle/admin.rb', line 157

def railtie_routes_url_helpers(include_path_helpers=true)
  Trestle.railtie_routes_url_helpers(include_path_helpers)
end

.root_actionObject


135
136
137
# File 'lib/trestle/admin.rb', line 135

def root_action
  :index
end

.route_nameObject


101
102
103
# File 'lib/trestle/admin.rb', line 101

def route_name
  "#{admin_name.tr('/', '_')}_admin"
end

.routesObject


143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/trestle/admin.rb', line 143

def routes
  admin = self

  Proc.new do
    scope controller: admin.controller_namespace, path: admin.options[:path] || admin.admin_name do
      get "", action: "index", as: admin.route_name

      admin.additional_routes.each do |block|
        instance_exec(&block)
      end
    end
  end
end

.tableObject

Deprecated: Use `tables` instead


45
46
47
# File 'lib/trestle/admin.rb', line 45

def table
  tables[:index]
end

.table=(table) ⇒ Object

Deprecated: Use `tables=` instead


50
51
52
# File 'lib/trestle/admin.rb', line 50

def table=(table)
  tables[:index] = table
end

.tablesObject


36
37
38
# File 'lib/trestle/admin.rb', line 36

def tables
  @tables ||= {}
end

.to_paramObject

Raises:

  • (NoMethodError)

127
128
129
# File 'lib/trestle/admin.rb', line 127

def to_param(*)
  raise NoMethodError, "#to_param called on non-resourceful admin. You may need to explicitly specify the admin."
end

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


89
90
91
92
93
94
# File 'lib/trestle/admin.rb', line 89

def translate(key, options={})
  defaults = [:"admin.#{i18n_key}.#{key}", :"admin.#{key}"]
  defaults << options[:default] if options[:default]

  I18n.t(defaults.shift, options.merge(default: defaults))
end

.validate!Object


165
166
167
# File 'lib/trestle/admin.rb', line 165

def validate!
  # No validations by default. This can be overridden in subclasses.
end

.view_path_prefixesObject


115
116
117
# File 'lib/trestle/admin.rb', line 115

def view_path_prefixes
  [view_path]
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

20
21
22
# File 'lib/trestle/admin.rb', line 20

def respond_to_missing?(name, include_private=false)
  self.class.respond_to?(name, include_private) || super
end