Module: Solidcrud::ApplicationHelper

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

Instance Method Summary collapse

Instance Method Details

#column_icon(column_name) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/solidcrud/application_helper.rb', line 3

def column_icon(column_name)
  case column_name.to_s
  when 'id'
    '<i class="fas fa-key"></i>'.html_safe
  when /name|title/
    '<i class="fas fa-tag"></i>'.html_safe
  when /email/
    '<i class="fas fa-envelope"></i>'.html_safe
  when /phone/
    '<i class="fas fa-phone"></i>'.html_safe
  when /address/
    '<i class="fas fa-map-marker-alt"></i>'.html_safe
  when /date|time/
    '<i class="fas fa-calendar"></i>'.html_safe
  when /created_at|updated_at/
    '<i class="fas fa-clock"></i>'.html_safe
  when /active|enabled|visible/
    '<i class="fas fa-check-circle"></i>'.html_safe
  when /count|amount|price|quantity/
    '<i class="fas fa-hashtag"></i>'.html_safe
  when /url|link/
    '<i class="fas fa-link"></i>'.html_safe
  when /description|content|body|text/
    '<i class="fas fa-file-alt"></i>'.html_safe
  else
    '<i class="fas fa-circle"></i>'.html_safe
  end
end

#column_visible?(column_name, model_name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'app/helpers/solidcrud/application_helper.rb', line 32

def column_visible?(column_name, model_name)
  # This will be handled by JavaScript, but we provide a default
  # All columns are visible by default
  true
end

#display_name_for_model(record) ⇒ Object



126
127
128
129
130
131
132
133
# File 'app/helpers/solidcrud/application_helper.rb', line 126

def display_name_for_model(record)
  [:name, :title, :label, :email, :username].each do |attr|
    return record.send(attr) if record.respond_to?(attr) && record.send(attr).present?
  end
  "#{record.class.name} ##{record.id}"
rescue
  "#{record.class.name} ##{record.id}"
end

#format_value(value, column_name, model_class) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/solidcrud/application_helper.rb', line 38

def format_value(value, column_name, model_class)
  return (:span, 'NULL', class: 'text-slate-400 italic font-medium') if value.nil?

  column = model_class.columns_hash[column_name.to_s]

  case column&.type
  when :boolean
    if value
      (:span, class: 'inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800') do
        '<i class="fas fa-check mr-1"></i> Yes'.html_safe
      end
    else
      (:span, class: 'inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-slate-100 text-slate-800') do
        '<i class="fas fa-times mr-1"></i> No'.html_safe
      end
    end
  when :datetime, :timestamp
    (:span, title: value.strftime('%Y-%m-%d %H:%M:%S %Z'), class: 'text-slate-900') do
      time_ago_in_words(value) + ' ago'
    end
  when :date
    value.strftime('%B %d, %Y')
  when :text
    truncate(value.to_s, length: 100)
  when :integer, :decimal, :float
    if column_name.to_s.include?('price') || column_name.to_s.include?('amount')
      number_to_currency(value)
    else
      number_with_delimiter(value)
    end
  else
    if value.to_s.length > 50
      (:span, title: value.to_s, class: 'text-slate-900') do
        truncate(value.to_s, length: 50)
      end
    else
      h(value.to_s)
    end
  end
rescue
  h(value.to_s)
end

#render_form_field(form, column_name, model_class) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/helpers/solidcrud/application_helper.rb', line 81

def render_form_field(form, column_name, model_class)
  column = model_class.columns_hash[column_name.to_s]

  case column&.type
  when :boolean
    form.check_box column_name, class: 'w-4 h-4 text-blue-600 bg-slate-100 border-slate-300 rounded focus:ring-blue-500 focus:ring-2'
  when :text
    form.text_area column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white resize-vertical', rows: 4
  when :date
    form.date_field column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
  when :datetime, :timestamp
    form.datetime_local_field column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
  when :integer
    if column_name.to_s.ends_with?('_id') && column_name.to_s != 'id'
      # Foreign key field
      association_name = column_name.to_s.gsub('_id', '')
      if model_class.reflect_on_association(association_name)
        associated_model = model_class.reflect_on_association(association_name).klass
        options = associated_model.all.map { |r| [display_name_for_model(r), r.id] }
        form.select column_name, options_for_select(options, form.object.send(column_name)),
                   { prompt: "Select #{association_name.humanize}" },
                   { class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white' }
      else
        form.number_field column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
      end
    else
      form.number_field column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
    end
  when :decimal, :float
    form.number_field column_name, step: 0.01, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
  else
    if column_name.to_s.include?('email')
      form.email_field column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
    elsif column_name.to_s.include?('phone')
      form.telephone_field column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
    elsif column_name.to_s.include?('url') || column_name.to_s.include?('link')
      form.url_field column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
    elsif column_name.to_s.include?('password')
      form.password_field column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
    else
      form.text_field column_name, class: 'w-full px-4 py-3 border border-slate-300 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all duration-200 bg-slate-50 focus:bg-white'
    end
  end
end

#solidcrud_javascript_path(filename = 'application.js') ⇒ Object



144
145
146
147
148
149
150
151
# File 'app/helpers/solidcrud/application_helper.rb', line 144

def solidcrud_javascript_path(filename = 'application.js')
  assets_enabled = Rails.application.config.respond_to?(:assets) && Rails.application.config.assets.enabled
  if Rails.application.config.api_only || !assets_enabled
    javascript_path(filename: filename)
  else
    "solidcrud/#{filename.sub('.js', '')}"
  end
end

#solidcrud_stylesheet_path(filename = 'application.css') ⇒ Object



135
136
137
138
139
140
141
142
# File 'app/helpers/solidcrud/application_helper.rb', line 135

def solidcrud_stylesheet_path(filename = 'application.css')
  assets_enabled = Rails.application.config.respond_to?(:assets) && Rails.application.config.assets.enabled
  if Rails.application.config.api_only || !assets_enabled
    stylesheet_path(filename: filename)
  else
    "solidcrud/#{filename.sub('.css', '')}"
  end
end