Module: SlashAdmin::ApplicationHelper

Defined in:
app/helpers/slash_admin/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#access?(s) ⇒ Boolean

is the current has access to at least one node

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/helpers/slash_admin/application_helper.rb', line 14

def access?(s)
  access = false
  if s[:sub_menu].present?
    s[:sub_menu].each do |sub|
      access = true if access_model?(sub)
    end
  end

  if s[:path].present?
    if can? s[:role], s[:role]
      access = true
    end
    if s[:role].present?
      if can? s[:role], s[:role]
        access = true
      end
    end
  end

  access
end

#access_model?(sub) ⇒ Boolean

is the current has access to model

Returns:

  • (Boolean)


37
38
39
40
# File 'app/helpers/slash_admin/application_helper.rb', line 37

def access_model?(sub)
  return false if cannot? :index, sub[:model]
  true
end

#admin_custom_field(form, attribute) ⇒ Object



205
206
207
208
# File 'app/helpers/slash_admin/application_helper.rb', line 205

def admin_custom_field(form, attribute)
  type = attribute[attribute.keys.first][:type].to_s
  render partial: "slash_admin/custom_fields/#{type}", locals: {f: form, a: attribute}
end

#admin_field(form, attribute) ⇒ Object

Form helper for generic field



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'app/helpers/slash_admin/application_helper.rb', line 211

def admin_field(form, attribute)
  object_class = form.object.class
  belongs_to_fields = object_class.reflect_on_all_associations(:belongs_to).map(&:name)
  has_many_fields = object_class.reflect_on_all_associations(:has_many).map(&:name)
  has_one_fields = object_class.reflect_on_all_associations(:has_one).map(&:name)

  # Handle custom field first and default after
  if attribute.is_a?(Hash)
    admin_custom_field(form, attribute)
  elsif belongs_to_fields.include?(attribute.to_sym)
    if form.object.class.nested_attributes_options.key?(attribute.to_sym)
      render partial: "slash_admin/fields/nested_belongs_to", locals: {f: form, a: attribute}
    else
      render partial: "slash_admin/fields/belongs_to", locals: {f: form, a: attribute}
    end
  elsif has_many_fields.include?(attribute.to_sym)
    # if has nested_attributes_options for has_many field
    if form.object.class.nested_attributes_options.key?(attribute.to_sym)
      render partial: "slash_admin/fields/nested_has_many", locals: {f: form, a: attribute}
    else
      render partial: "slash_admin/fields/has_many", locals: {f: form, a: attribute}
    end
  elsif has_one_fields.include?(attribute.to_sym)
    if form.object.class.nested_attributes_options.key?(attribute.to_sym)
      render partial: "slash_admin/fields/nested_has_one", locals: {f: form, a: attribute}
    else
      render partial: "slash_admin/fields/has_one", locals: {f: form, a: attribute}
    end
  elsif form.object.class&.uploaders&.key?(attribute.to_sym)
    render partial: "slash_admin/fields/carrierwave", locals: {f: form, a: attribute}
  else
    type = form.object.class.type_for_attribute(attribute.to_s).type.to_s
    if type == "date" || type == "datetime"
      render partial: "slash_admin/fields/date", locals: {f: form, a: attribute}
    else
      raise Exception.new("Unable to guess field_type for attribute: #{attribute} in model: #{object_class}") if type.blank?
      render partial: "slash_admin/fields/#{type}", locals: {f: form, a: attribute}
    end
  end
end

#class_name_from_association(obj, field_name) ⇒ Object

Raises:

  • (Exception)


42
43
44
45
46
47
48
49
50
# File 'app/helpers/slash_admin/application_helper.rb', line 42

def class_name_from_association(obj, field_name)
  [:belongs_to, :has_many, :has_one].each do |relation|
    obj.class.reflect_on_all_associations(relation).each do |association|
      return association.class_name if association.name == field_name.to_sym
    end
  end

  raise Exception.new("Unable to guess association for attribute: #{field_name} in model: #{obj}")
end

#errors?(form, field_name) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
# File 'app/helpers/slash_admin/application_helper.rb', line 81

def errors?(form, field_name)
  object = form.object
  if field_name.is_a?(Hash)
    field_name = field_name.keys.first
  end
  return false if object.errors.empty?
  return true unless object.errors.messages[field_name].blank?
end

#guess_field_type(object, attr) ⇒ Object

Automatic retrieve of field type object params can be a Model Class or a Model Instance boolean integer number decimal string text date datetime has_many belongs_to

Raises:

  • (Exception)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/helpers/slash_admin/application_helper.rb', line 151

def guess_field_type(object, attr)
  object_class = if object.class === Class
    object
  else
    object.class
  end

  belongs_to_fields = object_class.reflect_on_all_associations(:belongs_to).map(&:name)
  has_many_fields = object_class.reflect_on_all_associations(:has_many).map(&:name)
  has_one_fields = object_class.reflect_on_all_associations(:has_one).map(&:name)

  return if attr.is_a? Hash

  type = if object_class&.uploaders&.key?(attr.to_sym)
    "image"
  elsif belongs_to_fields.include?(attr.to_sym)
    "belongs_to"
  elsif has_many_fields.include?(attr.to_sym)
    "has_many"
  elsif has_one_fields.include?(attr.to_sym)
    "has_one"
  else
    object_class.type_for_attribute(attr.to_s).type.to_s
  end

  # Virtual field default string eg password
  return "string" if object_class.new.respond_to?(attr) && type.blank?

  # Raise exception if no type fouded
  raise Exception.new("Unable to guess field_type for attribute: #{attr} in model: #{object_class}") if type.blank?
  type
end

#object_label(a) ⇒ Object

Default label for object to string, title and name can be an attribute, a string or the model_class



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/helpers/slash_admin/application_helper.rb', line 117

def object_label(a)
  constantized_model = if a.is_a? Hash
    a.keys.first.to_s.singularize.classify.constantize
  elsif a.is_a? ActiveRecord::Base
    a
  else
    a.to_s.singularize.classify.constantize
  end

  constantized_model = constantized_model.new

  method = "to_s"
  object_label_methods.each do |m|
    method = m if constantized_model.respond_to?(m)
  end

  method
end

#object_label_methodsObject



111
112
113
# File 'app/helpers/slash_admin/application_helper.rb', line 111

def object_label_methods
  [:title, :name]
end

#orderable?(object, attr) ⇒ Boolean

Default available field_type handeled

Returns:

  • (Boolean)


101
102
103
104
# File 'app/helpers/slash_admin/application_helper.rb', line 101

def orderable?(object, attr)
  field_type = guess_field_type(object, attr)
  %w[boolean integer number decimal string text date datetime].include?(field_type)
end

#page_sub_title(content) ⇒ Object



9
10
11
# File 'app/helpers/slash_admin/application_helper.rb', line 9

def page_sub_title(content)
  content_for :page_sub_title, content
end

#page_title(content) ⇒ Object



5
6
7
# File 'app/helpers/slash_admin/application_helper.rb', line 5

def page_title(content)
  content_for :page_title, content
end

#required?(obj, field_name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/slash_admin/application_helper.rb', line 52

def required?(obj, field_name)
  if field_name.is_a?(Hash)
    field_hash = field_name
    field_name = field_hash.keys.first

    if field_hash[field_name].key?(:required)
      return field_hash[field_name][:required]
    end
  end

  obj.class.validators_on(field_name.to_s).any? { |v| v.is_a? ActiveModel::Validations::PresenceValidator }
end

#serialized_attributes_for(model) ⇒ Object

Parameters:

  • model

    @model_class



186
187
188
189
190
191
192
193
194
195
196
# File 'app/helpers/slash_admin/application_helper.rb', line 186

def serialized_attributes_for(model)
  serialized_columns = model.columns.select { |column|
    model.type_for_attribute(column.name).is_a?(
      ::ActiveRecord::Type::Serialized,
    )
  }

  serialized_columns.each_with_object({}) do |column, hash|
    hash[column.name.to_s] = model.type_for_attribute(column.name).coder
  end
end

#serialized_json_field?(model, attribute) ⇒ Boolean

Parameters:

  • model

    @model_class

  • attribute

    String

Returns:

  • (Boolean)


200
201
202
203
# File 'app/helpers/slash_admin/application_helper.rb', line 200

def serialized_json_field?(model, attribute)
  hash = serialized_attributes_for(model)
  hash.key?(attribute) && hash[attribute] == ::ActiveRecord::Coders::JSON
end

#show_errors(form, field_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/slash_admin/application_helper.rb', line 69

def show_errors(form, field_name)
  object = form.object
  if field_name.is_a?(Hash)
    field_name = field_name.keys.first
  end
  return [] if object.errors.empty?
  unless object.errors.messages[field_name].blank?
    return object.errors.messages[field_name]
  end
  []
end

#show_hidden_errorsObject



65
66
67
# File 'app/helpers/slash_admin/application_helper.rb', line 65

def show_hidden_errors
  @model.errors.messages.except(*@update_params)
end

#show_object(a) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'app/helpers/slash_admin/application_helper.rb', line 136

def show_object(a)
  method = "to_s"

  unless a.blank?
    object_label_methods.each do |m|
      method = m if a.respond_to?(m)
    end

    a.send(method)
  end
end

#sortable?(object, attr) ⇒ Boolean

By default all sortable fields are orderable

Returns:

  • (Boolean)


107
108
109
# File 'app/helpers/slash_admin/application_helper.rb', line 107

def sortable?(object, attr)
  orderable?(object, attr)
end

#toastr_bootstrapObject

Type must be ‘warning’ or ‘success’ or ‘error’



91
92
93
94
95
96
97
98
# File 'app/helpers/slash_admin/application_helper.rb', line 91

def toastr_bootstrap
  flash_messages = []
  flash.each do |type, message|
    text = '<script data-turbolinks-eval="false">toastr.' + type + '("' + message + '");</script>'
    flash_messages << text.html_safe if message
  end
  flash_messages.join("\n").html_safe
end