Class: Terrazzo::BaseDashboard
- Inherits:
-
Object
- Object
- Terrazzo::BaseDashboard
- Defined in:
- lib/terrazzo/base_dashboard.rb
Constant Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #attribute_hint(_attribute, _context = nil) ⇒ Object
- #attribute_label(attribute, _context = nil) ⇒ Object
- #attribute_type_for(attribute) ⇒ Object
- #attribute_types ⇒ Object
- #collection_attributes ⇒ Object
- #collection_bulk_actions(_view = nil) ⇒ Object
- #collection_cell_options(_attribute, _resource) ⇒ Object
- #collection_filter_label(filter_name) ⇒ Object
- #collection_filter_options(_view = nil) ⇒ Object
- #collection_filters ⇒ Object
- #collection_header_options(_attribute) ⇒ Object
- #collection_includes ⇒ Object
- #collection_item_actions(resource, view = nil) ⇒ Object
- #collection_row_options(_resource) ⇒ Object
- #collection_toolbar_actions(view = nil) ⇒ Object
- #csv_attributes ⇒ Object
- #csv_export_enabled? ⇒ Boolean
- #csv_filename ⇒ Object
- #csv_value(_attribute, value, _resource) ⇒ Object
- #display_resource(resource) ⇒ Object
- #empty_collection_message ⇒ Object
- #flatten_attributes(attrs) ⇒ Object
- #form_attributes(action = nil) ⇒ Object
- #has_many_attributes ⇒ Object
- #includes_for_attributes(attributes) ⇒ Object
- #layout_actions(_page, _view = nil, resource: nil) ⇒ Object
- #navigation_group ⇒ Object
- #navigation_group_order ⇒ Object
- #navigation_label ⇒ Object
- #navigation_order ⇒ Object
- #permitted_attributes(action = nil) ⇒ Object
- #search_attributes ⇒ Object
- #show_in_navigation? ⇒ Boolean
- #show_page_attributes ⇒ Object
Class Method Details
.model ⇒ Object
250 251 252 |
# File 'lib/terrazzo/base_dashboard.rb', line 250 def model name.chomp("Dashboard").constantize end |
.resource_name ⇒ Object
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_types ⇒ Object
7 8 9 |
# File 'lib/terrazzo/base_dashboard.rb', line 7 def attribute_types self.class::ATTRIBUTE_TYPES end |
#collection_attributes ⇒ Object
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 (_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 (_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_filters ⇒ Object
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 (_attribute) {} end |
#collection_includes ⇒ Object
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 (_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 (view = nil) return [] unless view && csv_export_enabled? [ { label: "Export CSV", url: csv_export_url(view), sg_visit: false, }, ] end |
#csv_attributes ⇒ Object
174 175 176 |
# File 'lib/terrazzo/base_dashboard.rb', line 174 def csv_attributes collection_attributes end |
#csv_export_enabled? ⇒ Boolean
170 171 172 |
# File 'lib/terrazzo/base_dashboard.rb', line 170 def csv_export_enabled? true end |
#csv_filename ⇒ Object
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_message ⇒ Object
195 196 197 |
# File 'lib/terrazzo/base_dashboard.rb', line 195 def "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_attributes ⇒ Object
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) = model.respond_to?(:reflect_on_attachment) && model.(include_target) next unless has_association || 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 |
#navigation_group ⇒ Object
205 206 207 208 209 |
# File 'lib/terrazzo/base_dashboard.rb', line 205 def self.class.const_defined?(:NAVIGATION_GROUP, false) ? self.class::NAVIGATION_GROUP : end |
#navigation_group_order ⇒ Object
217 218 219 220 221 |
# File 'lib/terrazzo/base_dashboard.rb', line 217 def self.class.const_defined?(:NAVIGATION_GROUP_ORDER, false) ? self.class::NAVIGATION_GROUP_ORDER : end |
#navigation_label ⇒ Object
199 200 201 202 203 |
# File 'lib/terrazzo/base_dashboard.rb', line 199 def self.class.const_defined?(:NAVIGATION_LABEL, false) ? self.class::NAVIGATION_LABEL : self.class.resource_name.pluralize end |
#navigation_order ⇒ Object
211 212 213 214 215 |
# File 'lib/terrazzo/base_dashboard.rb', line 211 def self.class.const_defined?(:NAVIGATION_ORDER, false) ? self.class::NAVIGATION_ORDER : 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_attributes ⇒ Object
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
223 224 225 226 227 |
# File 'lib/terrazzo/base_dashboard.rb', line 223 def self.class.const_defined?(:SHOW_IN_NAVIGATION, false) ? self.class::SHOW_IN_NAVIGATION : true end |
#show_page_attributes ⇒ Object
45 46 47 |
# File 'lib/terrazzo/base_dashboard.rb', line 45 def show_page_attributes self.class::SHOW_PAGE_ATTRIBUTES end |