Class: Terrazzo::BaseDashboard

Inherits:
Object
  • Object
show all
Defined in:
lib/terrazzo/base_dashboard.rb

Constant Summary collapse

Field =
Terrazzo::Field

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.modelObject



250
251
252
# File 'lib/terrazzo/base_dashboard.rb', line 250

def model
  name.chomp("Dashboard").constantize
end

.resource_nameObject



254
255
256
# File 'lib/terrazzo/base_dashboard.rb', line 254

def resource_name
  model.model_name.human
end

Instance Method Details

#attribute_hint(_attribute, _context = nil) ⇒ Object



75
76
77
# File 'lib/terrazzo/base_dashboard.rb', line 75

def attribute_hint(_attribute, _context = nil)
  nil
end

#attribute_label(attribute, _context = nil) ⇒ Object



71
72
73
# File 'lib/terrazzo/base_dashboard.rb', line 71

def attribute_label(attribute, _context = nil)
  attribute.to_s.humanize
end

#attribute_type_for(attribute) ⇒ Object



11
12
13
14
15
# File 'lib/terrazzo/base_dashboard.rb', line 11

def attribute_type_for(attribute)
  type = attribute_types[attribute]
  raise "Unknown attribute: #{attribute}" unless type
  type
end

#attribute_typesObject



7
8
9
# File 'lib/terrazzo/base_dashboard.rb', line 7

def attribute_types
  self.class::ATTRIBUTE_TYPES
end

#collection_attributesObject



36
37
38
39
40
41
42
43
# File 'lib/terrazzo/base_dashboard.rb', line 36

def collection_attributes
  attrs = self.class::COLLECTION_ATTRIBUTES
  if attrs.is_a?(Hash)
    attrs.values.flatten
  else
    attrs
  end
end

#collection_bulk_actions(_view = nil) ⇒ Object



162
163
164
# File 'lib/terrazzo/base_dashboard.rb', line 162

def collection_bulk_actions(_view = nil)
  []
end

#collection_cell_options(_attribute, _resource) ⇒ Object



79
80
81
# File 'lib/terrazzo/base_dashboard.rb', line 79

def collection_cell_options(_attribute, _resource)
  {}
end

#collection_filter_label(filter_name) ⇒ Object



125
126
127
# File 'lib/terrazzo/base_dashboard.rb', line 125

def collection_filter_label(filter_name)
  filter_name.to_s.humanize
end

#collection_filter_options(_view = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/terrazzo/base_dashboard.rb', line 114

def collection_filter_options(_view = nil)
  collection_filters.filter_map do |name, filter|
    next if filter.respond_to?(:arity) && filter.arity > 1

    {
      label: collection_filter_label(name),
      value: name.to_s,
    }
  end
end

#collection_filtersObject



109
110
111
112
# File 'lib/terrazzo/base_dashboard.rb', line 109

def collection_filters
  return {} unless self.class.const_defined?(:COLLECTION_FILTERS)
  self.class::COLLECTION_FILTERS
end

#collection_header_options(_attribute) ⇒ Object



83
84
85
# File 'lib/terrazzo/base_dashboard.rb', line 83

def collection_header_options(_attribute)
  {}
end

#collection_includesObject



91
92
93
# File 'lib/terrazzo/base_dashboard.rb', line 91

def collection_includes
  includes_for_attributes(collection_attributes)
end

#collection_item_actions(resource, view = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/terrazzo/base_dashboard.rb', line 129

def collection_item_actions(resource, view = nil)
  return [] unless view

  actions = []
  if (path = view.collection_action_path(resource, :show))
    actions << { label: "Show", url: path }
  end
  if (path = view.collection_action_path(resource, :edit))
    actions << { label: "Edit", url: path }
  end
  if (path = view.collection_action_path(resource, :destroy))
    actions << {
      label: "Destroy",
      url: path,
      method: "delete",
      confirm: "Are you sure?"
    }
  end
  actions
end

#collection_row_options(_resource) ⇒ Object



87
88
89
# File 'lib/terrazzo/base_dashboard.rb', line 87

def collection_row_options(_resource)
  {}
end

#collection_toolbar_actions(view = nil) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/terrazzo/base_dashboard.rb', line 150

def collection_toolbar_actions(view = nil)
  return [] unless view && csv_export_enabled?

  [
    {
      label: "Export CSV",
      url: csv_export_url(view),
      sg_visit: false,
    },
  ]
end

#csv_attributesObject



174
175
176
# File 'lib/terrazzo/base_dashboard.rb', line 174

def csv_attributes
  collection_attributes
end

#csv_export_enabled?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/terrazzo/base_dashboard.rb', line 170

def csv_export_enabled?
  true
end

#csv_filenameObject



178
179
180
# File 'lib/terrazzo/base_dashboard.rb', line 178

def csv_filename
  "#{self.class.resource_name.pluralize.parameterize}.csv"
end

#csv_value(_attribute, value, _resource) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/terrazzo/base_dashboard.rb', line 182

def csv_value(_attribute, value, _resource)
  case value
  when nil
    nil
  when Hash
    csv_hash_value(value)
  when Array
    value.map { |item| csv_value(nil, item, nil) }.join(", ")
  else
    value.to_s
  end
end

#display_resource(resource) ⇒ Object



67
68
69
# File 'lib/terrazzo/base_dashboard.rb', line 67

def display_resource(resource)
  "#{resource.class.name} ##{resource.id}"
end

#empty_collection_messageObject



195
196
197
# File 'lib/terrazzo/base_dashboard.rb', line 195

def empty_collection_message
  "No #{self.class.resource_name.pluralize.downcase} match the current view."
end

#flatten_attributes(attrs) ⇒ Object



49
50
51
# File 'lib/terrazzo/base_dashboard.rb', line 49

def flatten_attributes(attrs)
  attrs.is_a?(Hash) ? attrs.values.flatten : Array(attrs)
end

#form_attributes(action = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/terrazzo/base_dashboard.rb', line 17

def form_attributes(action = nil)
  case action
  when "new", "create"
    if self.class.const_defined?(:FORM_ATTRIBUTES_NEW)
      self.class::FORM_ATTRIBUTES_NEW
    else
      self.class::FORM_ATTRIBUTES
    end
  when "edit", "update"
    if self.class.const_defined?(:FORM_ATTRIBUTES_EDIT)
      self.class::FORM_ATTRIBUTES_EDIT
    else
      self.class::FORM_ATTRIBUTES
    end
  else
    self.class::FORM_ATTRIBUTES
  end
end

#has_many_attributesObject



63
64
65
# File 'lib/terrazzo/base_dashboard.rb', line 63

def has_many_attributes
  attribute_types.select { |_attr, type| type.field_type == "has_many" }.keys
end

#includes_for_attributes(attributes) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/terrazzo/base_dashboard.rb', line 95

def includes_for_attributes(attributes)
  attribute_set = Set.new(flatten_attributes(attributes))
  model = self.class.model
  attribute_types.each_with_object([]) do |(attr, type), includes|
    next unless attribute_set.include?(attr)
    next unless type.eager_load?
    include_target = type.eager_load_association(attr, model)
    has_association = model.reflect_on_association(include_target)
    has_attachment = model.respond_to?(:reflect_on_attachment) && model.reflect_on_attachment(include_target)
    next unless has_association || has_attachment
    includes << include_target
  end
end

#layout_actions(_page, _view = nil, resource: nil) ⇒ Object



166
167
168
# File 'lib/terrazzo/base_dashboard.rb', line 166

def layout_actions(_page, _view = nil, resource: nil)
  []
end


205
206
207
208
209
# File 'lib/terrazzo/base_dashboard.rb', line 205

def navigation_group
  self.class.const_defined?(:NAVIGATION_GROUP, false) ?
    self.class::NAVIGATION_GROUP :
    default_navigation_group
end


217
218
219
220
221
# File 'lib/terrazzo/base_dashboard.rb', line 217

def navigation_group_order
  self.class.const_defined?(:NAVIGATION_GROUP_ORDER, false) ?
    self.class::NAVIGATION_GROUP_ORDER :
    navigation_group
end


199
200
201
202
203
# File 'lib/terrazzo/base_dashboard.rb', line 199

def navigation_label
  self.class.const_defined?(:NAVIGATION_LABEL, false) ?
    self.class::NAVIGATION_LABEL :
    self.class.resource_name.pluralize
end


211
212
213
214
215
# File 'lib/terrazzo/base_dashboard.rb', line 211

def navigation_order
  self.class.const_defined?(:NAVIGATION_ORDER, false) ?
    self.class::NAVIGATION_ORDER :
    navigation_label
end

#permitted_attributes(action = nil) ⇒ Object



53
54
55
56
57
# File 'lib/terrazzo/base_dashboard.rb', line 53

def permitted_attributes(action = nil)
  flatten_attributes(form_attributes(action)).map do |attr|
    attribute_type_for(attr).permitted_attribute(attr, model_class: self.class.model)
  end
end

#search_attributesObject



59
60
61
# File 'lib/terrazzo/base_dashboard.rb', line 59

def search_attributes
  attribute_types.select { |_attr, type| type.searchable? }.keys
end

#show_in_navigation?Boolean

Returns:

  • (Boolean)


223
224
225
226
227
# File 'lib/terrazzo/base_dashboard.rb', line 223

def show_in_navigation?
  self.class.const_defined?(:SHOW_IN_NAVIGATION, false) ?
    self.class::SHOW_IN_NAVIGATION :
    true
end

#show_page_attributesObject



45
46
47
# File 'lib/terrazzo/base_dashboard.rb', line 45

def show_page_attributes
  self.class::SHOW_PAGE_ATTRIBUTES
end