Class: Administrate::BaseDashboard

Inherits:
Object
  • Object
show all
Includes:
Administrate
Defined in:
lib/administrate/base_dashboard.rb

Constant Summary collapse

DASHBOARD_SUFFIX =
"Dashboard".freeze

Constants included from Administrate

VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Administrate

deprecator, warn_of_deprecated_authorization_method, warn_of_deprecated_method, warn_of_deprecated_option, warn_of_missing_resource_class

Class Method Details

.modelObject



25
26
27
# File 'lib/administrate/base_dashboard.rb', line 25

def model
  to_s.chomp(DASHBOARD_SUFFIX).classify.constantize
end

.resource_name(opts) ⇒ Object



29
30
31
# File 'lib/administrate/base_dashboard.rb', line 29

def resource_name(opts)
  model.model_name.human(opts)
end

Instance Method Details

#all_attributesObject



50
51
52
# File 'lib/administrate/base_dashboard.rb', line 50

def all_attributes
  attribute_types.keys
end

#attribute_type_for(attribute_name) ⇒ Object



38
39
40
41
42
# File 'lib/administrate/base_dashboard.rb', line 38

def attribute_type_for(attribute_name)
  attribute_types.fetch(attribute_name) do
    fail attribute_not_found_message(attribute_name)
  end
end

#attribute_typesObject



34
35
36
# File 'lib/administrate/base_dashboard.rb', line 34

def attribute_types
  self.class::ATTRIBUTE_TYPES
end

#attribute_types_for(attribute_names) ⇒ Object



44
45
46
47
48
# File 'lib/administrate/base_dashboard.rb', line 44

def attribute_types_for(attribute_names)
  attribute_names.each_with_object({}) do |name, attributes|
    attributes[name] = attribute_type_for(name)
  end
end

#collection_attributesObject



92
93
94
95
96
97
98
# File 'lib/administrate/base_dashboard.rb', line 92

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

#collection_includesObject



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

def collection_includes
  attribute_includes(collection_attributes)
end

#display_resource(resource) ⇒ Object



106
107
108
# File 'lib/administrate/base_dashboard.rb', line 106

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

#form_attributes(action = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/administrate/base_dashboard.rb', line 54

def form_attributes(action = nil)
  action =
    case action
    when "update" then "edit"
    when "create" then "new"
    else action
    end
  specific_form_attributes_for(action) || self.class::FORM_ATTRIBUTES
end

#item_associationsObject



120
121
122
123
124
125
126
127
# File 'lib/administrate/base_dashboard.rb', line 120

def item_associations
  attributes = if show_page_attributes.is_a?(Hash)
    show_page_attributes.values.flatten
  else
    show_page_attributes
  end
  attribute_associated attributes
end

#item_includesObject



114
115
116
117
118
# File 'lib/administrate/base_dashboard.rb', line 114

def item_includes
  # Deprecated, internal usage has moved to #item_associations
  Administrate.warn_of_deprecated_method(self.class, :item_includes)
  attribute_includes(show_page_attributes)
end

#permitted_attributes(action = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/administrate/base_dashboard.rb', line 72

def permitted_attributes(action = nil)
  attributes = form_attributes action

  if attributes.is_a? Hash
    attributes = attributes.values.flatten
  end

  attributes.map do |attr|
    attribute_types[attr].permitted_attribute(
      attr,
      resource_class: self.class.model,
      action: action
    )
  end.uniq
end

#search_attributesObject



100
101
102
103
104
# File 'lib/administrate/base_dashboard.rb', line 100

def search_attributes
  attribute_types.keys.select do |attribute|
    attribute_types[attribute].searchable?
  end
end

#show_page_attributesObject



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

def show_page_attributes
  self.class::SHOW_PAGE_ATTRIBUTES
end

#specific_form_attributes_for(action) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/administrate/base_dashboard.rb', line 64

def specific_form_attributes_for(action)
  return unless action

  cname = "FORM_ATTRIBUTES_#{action.upcase}"

  self.class.const_get(cname) if self.class.const_defined?(cname)
end