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



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

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

.resource_nameObject



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

def resource_name
  model.model_name.human
end

Instance Method Details

#attribute_type_for(attribute) ⇒ Object



9
10
11
12
13
# File 'lib/terrazzo/base_dashboard.rb', line 9

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

#attribute_typesObject



5
6
7
# File 'lib/terrazzo/base_dashboard.rb', line 5

def attribute_types
  self.class::ATTRIBUTE_TYPES
end

#collection_attributesObject



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

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

#collection_filtersObject



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

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

#collection_includesObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/terrazzo/base_dashboard.rb', line 65

def collection_includes
  collection_attr_set = Set.new(collection_attributes)
  model = self.class.model
  attribute_types.each_with_object([]) do |(attr, type), includes|
    next unless collection_attr_set.include?(attr)
    next unless type.eager_load?
    has_association = model.reflect_on_association(attr)
    has_attachment = model.respond_to?(:reflect_on_attachment) && model.reflect_on_attachment(attr)
    next unless has_association || has_attachment
    includes << attr
  end
end

#display_resource(resource) ⇒ Object



61
62
63
# File 'lib/terrazzo/base_dashboard.rb', line 61

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

#flatten_attributes(attrs) ⇒ Object



47
48
49
# File 'lib/terrazzo/base_dashboard.rb', line 47

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

#form_attributes(action = nil) ⇒ Object



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

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

#permitted_attributes(action = nil) ⇒ Object



51
52
53
54
55
# File 'lib/terrazzo/base_dashboard.rb', line 51

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



57
58
59
# File 'lib/terrazzo/base_dashboard.rb', line 57

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

#show_page_attributesObject



43
44
45
# File 'lib/terrazzo/base_dashboard.rb', line 43

def show_page_attributes
  self.class::SHOW_PAGE_ATTRIBUTES
end