Module: CtTableFor::ApplicationHelper
- Defined in:
- app/helpers/ct_table_for/application_helper.rb
Instance Method Summary collapse
- #button_for_custom_action(record, options, extras) ⇒ Object
- #class_for_action(action, options) ⇒ Object
- #label_for_action(action, icons = true) ⇒ Object
- #parse_extras(extras) ⇒ Object
- #row_data_link(record, options) ⇒ Object
-
#table_for(model, collection, options: {}) ⇒ Object
RWD Table use as: table_for Model, @collection, options: {} options: { actions: { buttons: %w(show, edit)}, // Hash: with array of buttons for actions premodel: [:bo, :admin], // Array: of symbols for nested namespaces/models icons: true // Boolean: if true show actions as icons } attributes: %(name:sortable email), // Array: of model attibutes to show in table // with extra parameter ‘:` add cell options (sort, image size) // if the attribute is a relation pass the attribute to show // if the attribute has an association will show a number with the count id: “my-id”, // String: adds custom id to <table> element class: “my-custom-css”, // String: add custom class to <table> element tr_class: “my-custom-css” // String: add custom class to <tr> element btn_class: { // Hash: add custom class to action buttons show: “my-custom-css”, edit: “my-custom-css” } clickable: true || Array // Boolean or Array of nested resources for polymorphic_url }.
- #table_for_actions(record, options: {}) ⇒ Object
- #table_for_attributes(model, options) ⇒ Object
- #table_for_cell(model, record, attribute, cell_options: {}) ⇒ Object
- #table_for_cell_for_image(record, attribute, cell_options: {}) ⇒ Object
- #table_for_cell_for_locale(model, attribute, value, cell_options: {}) ⇒ Object
- #table_for_content(model, collection, options: {}) ⇒ Object
- #table_for_header(model, has_actions: false, options: {}) ⇒ Object
- #uri?(string) ⇒ Boolean
Instance Method Details
#button_for_custom_action(record, options, extras) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 225 def record, , extras parsed_extras = parse_extras(extras) # `icons` is a Boolean-String option to limit `icon` option label = if parsed_extras[:icons].to_s == "false" parsed_extras[:title].presence || "" else %Q{<i class="#{CtTableFor.table_for_icon_font_base_class} #{CtTableFor.table_for_icon_font_base_class}-#{parsed_extras[:icon]}"></i>} end ancestors_list = parsed_extras[:ancestors].presence || "" ancestors = ancestors_list.split(",").map do |ancestor| record.send(ancestor) end custom_action_class = %Q{#{CtTableFor.table_for_default_action_base_class} #{parsed_extras[:class]}} link_to(label.html_safe, polymorphic_path([parsed_extras[:link], *ancestors, record]), class: custom_action_class, method: parsed_extras[:method], title: parsed_extras[:title]) end |
#class_for_action(action, options) ⇒ Object
221 222 223 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 221 def class_for_action action, %Q{#{CtTableFor.table_for_default_action_base_class} #{options.dig(:btn_class, action.to_sym) || CtTableFor.table_for_action_class[action.to_sym]}} end |
#label_for_action(action, icons = true) ⇒ Object
213 214 215 216 217 218 219 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 213 def label_for_action action, icons = true label = I18n.t(action.to_sym, scope: [:table_for, :buttons]).capitalize if icons != false label = %Q{<i class="#{CtTableFor.table_for_icon_font_base_class} #{CtTableFor.table_for_icon_font_base_class}-#{CtTableFor.table_for_action_icons[action.to_sym]}"></i>} end label end |
#parse_extras(extras) ⇒ Object
255 256 257 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 255 def parse_extras(extras) Hash[extras.collect { |extra| [extra.split(":").first, extra.split(":").last] } ].with_indifferent_access end |
#row_data_link(record, options) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 93 def row_data_link(record, ) return unless [:clickable] if [:clickable].kind_of?(Array) nested_resources = ([:clickable] || []) + [record] else nested_resources = record end "data-link =" << polymorphic_url(nested_resources) end |
#table_for(model, collection, options: {}) ⇒ Object
RWD Table use as: table_for Model, @collection, options: {} options:
actions: {
buttons: %w(show, edit), // Hash: with array of for actions
premodel: [:bo, :admin], // Array: of symbols for nested namespaces/models
icons: true // Boolean: if true show actions as icons
}
attributes: %(name:sortable email), // Array: of model attibutes to show in table
// with extra parameter `:` add cell (sort, image size)
// if the attribute is a relation pass the attribute to show
// if the attribute has an association will show a number with the count
id: "my-id", // String: adds custom id to <table> element
class: "my-custom-css", // String: add custom class to <table> element
tr_class: "my-custom-css" // String: add custom class to <tr> element
btn_class: { // Hash: add custom class to action
show: "my-custom-css",
edit: "my-custom-css"
}
clickable: true || Array // Boolean or Array of nested resources for polymorphic_url
}
29 30 31 32 33 34 35 36 37 38 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 29 def table_for model, collection, options: {} custom_id = [:id].present? ? %Q{id="#{options[:id]}"} : "" html = %Q{<div class="table-for-wrapper #{CtTableFor.table_for_wrapper_default_class}">} html << %Q{<table #{custom_id} class="table-for #{CtTableFor.table_for_default_class} #{options[:class]} #{("table-clickable") if options[:clickable]}">} html << table_for_header(model, has_actions: [:actions].present?, options: ) html << table_for_content(model, collection, options: ) html << %Q{</table>} html << %Q{</div>} html.html_safe end |
#table_for_actions(record, options: {}) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 179 def table_for_actions(record, options: {} ) return "" if [:actions].blank? html = "" html << %Q{<td data-link-enabled="false" class="#{CtTableFor.table_for_td_default_prefix_class}-actions">} html << %Q{<div class="btn-group btn-group-sm" role="group" aria-label="#{I18n.t(:actions, scope: [:table_for]).capitalize}">} nesting = ([:actions][:premodel] || []) + [record] = [:actions][:buttons].map{ |b| b.split("|")} .each do |action, *extras| if defined?(CanCanCan) && cannot?(action.to_sym, record) html << "" else case action.to_sym when :show html << link_to(label_for_action(action, [:actions][:icons]).html_safe, polymorphic_path(nesting), class: class_for_action(action, )) when :edit html << link_to(label_for_action(action, [:actions][:icons]).html_safe, edit_polymorphic_path(nesting), class: class_for_action(action, )) when :destroy html << link_to(label_for_action(action, [:actions][:icons]).html_safe, polymorphic_path(nesting), method: :delete, class: class_for_action(action, ), data: { confirm: I18n.t('table_for.messages.are_you_sure').capitalize }) when :custom html << (record, , extras) else # TODO: # nesting_custom = nesting + btn_options[0] # label = icon CtTableFor.table_for_action_icons[:custom] if options[:actions][:icons] != false and defined?(FontAwesome) # html << link_to(label, polymorphic_path(nesting_custom), class: "btn btn-default btn-sm") end end end html << %Q{</div>} html << %Q{</td>} html.html_safe end |
#table_for_attributes(model, options) ⇒ Object
40 41 42 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 40 def table_for_attributes model, [:attributes] || model.attribute_names end |
#table_for_cell(model, record, attribute, cell_options: {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 103 def table_for_cell model, record, attribute, cell_options: {} html = "" value = if record.respond_to?(attribute.to_sym) record.send(attribute.to_sym) elsif self.respond_to?(attribute.to_sym) self.send(attribute.to_sym, record) else nil end html << %Q{<td data-title="#{model.human_attribute_name("#{attribute}")}" class="#{CtTableFor.table_for_td_default_prefix_class}-#{attribute}">} case value when NilClass html << %Q{<i class="fa fa-minus text-muted"></i>} when TrueClass, FalseClass html << %Q{<i class="fa #{value ? "fa-check text-success" : "fa-times text-danger"}"></i>} when Numeric if .include? "currency" html << number_to_currency(value) elsif .include? "percentage" html << number_to_percentage(value, precision: CtTableFor.table_for_numeric_percentage_precision) else html << %Q{<code>#{value}</code>} end when ActiveSupport::TimeWithZone # TODO: value.in_time_zone html << %Q{<code>#{value.strftime("%d/%m/%Y %H:%M:%S")}</code>} when Date html << %Q{#{value.strftime("%d/%m/%Y")}} when Time # TODO: value.in_time_zone html << %Q{<code>#{value.strftime("%H:%M:%S")}</code>} when ActiveRecord::Base if .present? html << %Q{#{value.send cell_options[0]}} else html << %{#{(value.try(:title) || value.try(:name))}} end when ActiveRecord::Associations::CollectionProxy html << %Q{#{value.count}} else if uri?(value) html << link_to(value, value) elsif defined?(Paperclip) and value.is_a?(Paperclip::Attachment) html << table_for_cell_for_image( record, attribute, cell_options: ) else if .include? "l" html << table_for_cell_for_locale(model, attribute, value) elsif .include? "no_truncate" html << value.to_s else html << value.to_s.truncate( CtTableFor.table_for_truncate_length, separator: CtTableFor.table_for_truncate_separator, omission: CtTableFor.table_for_truncate_omission ) end end end html << %Q{</td>} html.html_safe end |
#table_for_cell_for_image(record, attribute, cell_options: {}) ⇒ Object
166 167 168 169 170 171 172 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 166 def table_for_cell_for_image record, attribute, cell_options: {} html = "" size = .select{ |opt| ["thumb", "original", "small", "medium"].include? opt }.first || "thumb" html << image_tag(record.send(attribute).url(size), class: CtTableFor.table_for_cell_for_image_image_class, style: "max-height: 100px;") html.html_safe end |
#table_for_cell_for_locale(model, attribute, value, cell_options: {}) ⇒ Object
174 175 176 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 174 def table_for_cell_for_locale model, attribute, value, cell_options: {} html = model.human_attribute_name("#{attribute.underscore}.#{value.underscore}") end |
#table_for_content(model, collection, options: {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 68 def table_for_content model, collection, options: {} html = "" html << %Q{<tbody>} if collection.present? custom_tr_class = [:tr_class].present? ? %Q{class="#{options[:tr_class]}"} : "" collection.each do |record| html << %Q{<tr data-colection-id="#{record.try(:id)}" #{custom_tr_class} #{row_data_link(record, options)}>} table_for_attributes(model, ).each do |attribute| attribute, *params = attribute.split(":") html << table_for_cell( model, record, attribute, cell_options: params ) end html << table_for_actions( record, options: ) if [:actions].present? html << %Q{</tr>} end else html << %Q{<tr>} html << %Q{<td colspan=#{options[:attributes].size + 1}>} html << I18n.t("table_for.messages.no_records") html << %Q{</td>} html << %Q{</tr>} end html << %Q{</tbody>} html.html_safe end |
#table_for_header(model, has_actions: false, options: {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 44 def table_for_header model, has_actions: false, options: {} html = "" html << %Q{<thead>} html << %Q{<tr>} table_for_attributes(model, ).each do |attribute| html << %Q{<th>} attribute, *params = attribute.split(":") html << if defined?(Ransack) and params.include? "sortable" if params.length > 1 && (params & ['l', 'currency']).empty? sort_link(@q, "#{attribute}_#{params.first}", I18n.t("#{attribute.to_s.underscore}_#{params.first}", scope: [:activerecord, :attributes, model.to_s.underscore]).capitalize ) else sort_link(@q, attribute, I18n.t("#{attribute}", scope: [:activerecord, :attributes, model.to_s.underscore]).capitalize ) end else model.human_attribute_name("#{attribute}") end html << %Q{</th>} end html << %Q{<th class="actions">#{I18n.t(:actions, scope: [:table_for]).capitalize}</th>} if has_actions html << %Q{</tr>} html << %Q{</thead>} html.html_safe end |
#uri?(string) ⇒ Boolean
245 246 247 248 249 250 251 252 253 |
# File 'app/helpers/ct_table_for/application_helper.rb', line 245 def uri?(string) # http://ruby-doc.org/stdlib-2.4.0/libdoc/uri/rdoc/URI.html uri = URI.parse(string) %w( http https ).include?(uri.scheme) rescue URI::BadURIError false rescue URI::InvalidURIError false end |