ct_table_for 1.0.2 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee4c048a968042ebb3909b69a45ae079082698350af7714678e032a6f59cddb8
4
- data.tar.gz: bea7445d0dd0cdc74e6b9df76de9d59152bc4fd5fdbddface41e3670db9e43dc
3
+ metadata.gz: 4ed64782cf3f8a5963571ae9f299b2e5638119f12d39a500bc3e8bcca41a4486
4
+ data.tar.gz: 6b2fee9d9181d27a1626087d0936434938211beeb1db8b5964ff96e40df5c479
5
5
  SHA512:
6
- metadata.gz: 3797d1adc5fa0ee49b510a74cbafcd8f8d9be35b0403fad2628ee0d18f6947ad14bb752339a2225ca5de462b8bb34a340d9a1a6fea75e9903c6ba5dd2633a5e4
7
- data.tar.gz: 411971b3b1452bc20d54714000eee5e73f122520b7941bcbca377aa392cb5758e179348b42cdc767fe312c3f86b61e7b0684497055a0ef4a019062e8970864a7
6
+ metadata.gz: d699345c49749cd4e5f2dd1e679195cf0ec1936cbaa911ba74b487315d3872296a016b102d43ffb084049b6a2decf8d8d46a0ab7e7742b45eae0578b7e8ae9cb
7
+ data.tar.gz: 7545d61265dcf373140a8cf46d828b1e2f2699676742c95f1a0350300cf42f47a39fbe54adf54bed6678eafabd24de06d2aced9652af5aefc0ef7df41105fd68
data/README.md CHANGED
@@ -84,6 +84,7 @@ CtTableFor.setup do |config|
84
84
  config.table_for_truncate_length = 50
85
85
  config.table_for_truncate_separator = " "
86
86
  config.table_for_truncate_omission = "..."
87
+ config.table_for_td_default_prefix_class = "td-item"
87
88
  end
88
89
  ```
89
90
  You can also define the breakpoint in your `sass` before importing `table_for`:
@@ -1,4 +1,4 @@
1
- $table-for-breakpoint: <%= CtTableFor.table_for_breakpoint %> !default;
1
+ $table-for-breakpoint: <%= CtTableFor.config.table_for_breakpoint %> !default;
2
2
 
3
3
  @media (max-width: $table-for-breakpoint) {
4
4
  // table as cards
@@ -6,6 +6,7 @@ module CtTableFor
6
6
  # RWD Table
7
7
  # use as: table_for Model, @collection, options: {}
8
8
  # options: {
9
+ # style: "lambda {|model| method(model)}" // String: adds a method for css class
9
10
  # actions: {
10
11
  # buttons: %w(show, edit)}, // Hash: with array of buttons for actions
11
12
  # premodel: [:bo, :admin], // Array: of symbols for nested namespaces/models
@@ -28,8 +29,8 @@ module CtTableFor
28
29
 
29
30
  def table_for model, collection, options: {}
30
31
  custom_id = options[:id].present? ? %Q{id="#{options[:id]}"} : ""
31
- html = %Q{<div class="table-for-wrapper #{CtTableFor.table_for_wrapper_default_class}">}
32
- html << %Q{<table #{custom_id} class="table-for #{CtTableFor.table_for_default_class} #{options[:class]} #{("table-clickable") if options[:clickable]}">}
32
+ html = %Q{<div class="table-for-wrapper #{CtTableFor.config.table_for_wrapper_default_class}">}
33
+ html << %Q{<table #{custom_id} class="table-for #{CtTableFor.config.table_for_default_class} #{options[:class]} #{("table-clickable") if options[:clickable]}">}
33
34
  html << table_for_header(model, has_actions: options[:actions].present?, options: options)
34
35
  html << table_for_content(model, collection, options: options)
35
36
  html << %Q{</table>}
@@ -49,7 +50,7 @@ module CtTableFor
49
50
  html << %Q{<th>}
50
51
  attribute, *params = attribute.split(":")
51
52
  html << if defined?(Ransack) and params.include? "sortable"
52
- if params.length > 1 && !params.include?("l")
53
+ if params.length > 1 && (params & ['l', 'currency']).empty?
53
54
  sort_link(@q, "#{attribute}_#{params.first}", I18n.t("#{attribute.to_s.underscore}_#{params.first}", scope: [:activerecord, :attributes, model.to_s.underscore]).capitalize )
54
55
  else
55
56
  sort_link(@q, attribute, I18n.t("#{attribute}", scope: [:activerecord, :attributes, model.to_s.underscore]).capitalize )
@@ -69,9 +70,9 @@ module CtTableFor
69
70
  html = ""
70
71
  html << %Q{<tbody>}
71
72
  if collection.present?
72
- custom_tr_class = options[:tr_class].present? ? %Q{class="#{options[:tr_class]}"} : ""
73
73
  collection.each do |record|
74
- html << %Q{<tr data-colection-id="#{record.try(:id)}" #{custom_tr_class} #{row_data_link(record, options)}>}
74
+ css_classes = get_css_classes(record, options)
75
+ html << %Q{<tr data-colection-id="#{record.try(:id)}" #{css_classes} #{row_data_link(record, options)}>}
75
76
  table_for_attributes(model, options).each do |attribute|
76
77
  attribute, *params = attribute.split(":")
77
78
  html << table_for_cell( model, record, attribute, cell_options: params )
@@ -110,7 +111,7 @@ module CtTableFor
110
111
  nil
111
112
  end
112
113
 
113
- html << %Q{<td data-title="#{model.human_attribute_name("#{attribute}")}">}
114
+ html << %Q{<td data-title="#{model.human_attribute_name("#{attribute}")}" class="#{CtTableFor.config.table_for_td_default_prefix_class}-#{attribute}">}
114
115
  case value
115
116
  when NilClass
116
117
  html << %Q{<i class="fa fa-minus text-muted"></i>}
@@ -120,13 +121,15 @@ module CtTableFor
120
121
  if cell_options.include? "currency"
121
122
  html << number_to_currency(value)
122
123
  elsif cell_options.include? "percentage"
123
- html << number_to_percentage(value, precision: CtTableFor.table_for_numeric_percentage_precision)
124
+ html << number_to_percentage(value, precision: CtTableFor.config.table_for_numeric_percentage_precision)
124
125
  else
125
- html << %Q{<code>#{value}</code>}
126
+ html << %Q{#{number_with_delimiter(value)}}
126
127
  end
127
128
  when ActiveSupport::TimeWithZone
128
129
  # TODO: value.in_time_zone
129
130
  html << %Q{<code>#{value.strftime("%d/%m/%Y %H:%M:%S")}</code>}
131
+ when Date
132
+ html << %Q{#{value.strftime("%d/%m/%Y")}}
130
133
  when Time
131
134
  # TODO: value.in_time_zone
132
135
  html << %Q{<code>#{value.strftime("%H:%M:%S")}</code>}
@@ -139,7 +142,9 @@ module CtTableFor
139
142
  when ActiveRecord::Associations::CollectionProxy
140
143
  html << %Q{#{value.count}}
141
144
  else
142
- if uri?(value)
145
+ if defined?(ActiveStorage) && value.is_a?(ActiveStorage::Attached::One)
146
+ html << table_for_cell_for_image( record, attribute, cell_options: cell_options )
147
+ elsif uri?(value)
143
148
  html << link_to(value, value)
144
149
  elsif defined?(Paperclip) and value.is_a?(Paperclip::Attachment)
145
150
  html << table_for_cell_for_image( record, attribute, cell_options: cell_options )
@@ -150,9 +155,9 @@ module CtTableFor
150
155
  html << value.to_s
151
156
  else
152
157
  html << value.to_s.truncate(
153
- CtTableFor.table_for_truncate_length,
154
- separator: CtTableFor.table_for_truncate_separator,
155
- omission: CtTableFor.table_for_truncate_omission
158
+ CtTableFor.config.table_for_truncate_length,
159
+ separator: CtTableFor.config.table_for_truncate_separator,
160
+ omission: CtTableFor.config.table_for_truncate_omission
156
161
  )
157
162
  end
158
163
  end
@@ -164,8 +169,14 @@ module CtTableFor
164
169
  def table_for_cell_for_image record, attribute, cell_options: {}
165
170
  html = ""
166
171
  size = cell_options.select{ |opt| ["thumb", "original", "small", "medium"].include? opt }.first || "thumb"
172
+ value = record.send(attribute)
173
+ return html unless value.present?
167
174
 
168
- html << image_tag(record.send(attribute).url(size), class: CtTableFor.table_for_cell_for_image_image_class, style: "max-height: 100px;")
175
+ if value.is_a?(ActiveStorage::Attached::One)
176
+ html << image_tag(value, class: CtTableFor.config.table_for_cell_for_image_image_class, style: "max-height: 100px;")
177
+ else
178
+ html << image_tag(value.url(size), class: CtTableFor.config.table_for_cell_for_image_image_class, style: "max-height: 100px;")
179
+ end
169
180
  html.html_safe
170
181
  end
171
182
 
@@ -177,7 +188,7 @@ module CtTableFor
177
188
  def table_for_actions(record, options: {} )
178
189
  return "" if options[:actions].blank?
179
190
  html = ""
180
- html << %Q{<td data-link-enabled="false">}
191
+ html << %Q{<td data-link-enabled="false" class="#{CtTableFor.config.table_for_td_default_prefix_class}-actions">}
181
192
  html << %Q{<div class="btn-group btn-group-sm" role="group" aria-label="#{I18n.t(:actions, scope: [:table_for]).capitalize}">}
182
193
  nesting = (options[:actions][:premodel] || []) + [record]
183
194
  buttons = options[:actions][:buttons].map{ |b| b.split("|")}
@@ -189,16 +200,18 @@ module CtTableFor
189
200
  when :show
190
201
  html << link_to(label_for_action(action, options[:actions][:icons]).html_safe, polymorphic_path(nesting), class: class_for_action(action, options))
191
202
  when :edit
192
- html << link_to(label_for_action(action, options[:actions][:icons]).html_safe, edit_polymorphic_path(nesting), class: class_for_action(action, options))
203
+ url_helper = options.dig(:actions, :urls, :edit) || lambda{|obj| edit_polymorphic_path(nesting)}
204
+ html << link_to(label_for_action(action, options[:actions][:icons]).html_safe, url_helper.(nesting), class: class_for_action(action, options))
193
205
  when :destroy
194
- html << link_to(label_for_action(action, options[:actions][:icons]).html_safe, polymorphic_path(nesting),
206
+ url_helper = options.dig(:actions, :urls, :destroy) || lambda{|obj| polymorphic_path(nesting)}
207
+ html << link_to(label_for_action(action, options[:actions][:icons]).html_safe, url_helper.(nesting),
195
208
  method: :delete, class: class_for_action(action, options), data: { confirm: I18n.t('table_for.messages.are_you_sure').capitalize })
196
209
  when :custom
197
210
  html << button_for_custom_action(record, options, extras)
198
211
  else
199
212
  # TODO:
200
213
  # nesting_custom = nesting + btn_options[0]
201
- # label = icon CtTableFor.table_for_action_icons[:custom] if options[:actions][:icons] != false and defined?(FontAwesome)
214
+ # label = icon CtTableFor.config.table_for_action_icons[:custom] if options[:actions][:icons] != false and defined?(FontAwesome)
202
215
  # html << link_to(label, polymorphic_path(nesting_custom), class: "btn btn-default btn-sm")
203
216
  end
204
217
  end
@@ -211,13 +224,13 @@ module CtTableFor
211
224
  def label_for_action action, icons = true
212
225
  label = I18n.t(action.to_sym, scope: [:table_for, :buttons]).capitalize
213
226
  if icons != false
214
- 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>}
227
+ label = %Q{<i class="#{CtTableFor.config.table_for_icon_font_base_class} #{CtTableFor.config.table_for_icon_font_base_class}-#{CtTableFor.config.table_for_action_icons[action.to_sym]}"></i>}
215
228
  end
216
229
  label
217
230
  end
218
231
 
219
232
  def class_for_action action, options
220
- %Q{#{CtTableFor.table_for_default_action_base_class} #{options.dig(:btn_class, action.to_sym) || CtTableFor.table_for_action_class[action.to_sym]}}
233
+ %Q{#{CtTableFor.config.table_for_default_action_base_class} #{options.dig(:btn_class, action.to_sym) || CtTableFor.config.table_for_action_class[action.to_sym]}}
221
234
  end
222
235
 
223
236
  def button_for_custom_action record, options, extras
@@ -226,18 +239,20 @@ module CtTableFor
226
239
  label = if parsed_extras[:icons].to_s == "false"
227
240
  parsed_extras[:title].presence || ""
228
241
  else
229
- %Q{<i class="#{CtTableFor.table_for_icon_font_base_class} #{CtTableFor.table_for_icon_font_base_class}-#{parsed_extras[:icon]}"></i>}
242
+ %Q{<i class="#{CtTableFor.config.table_for_icon_font_base_class} #{CtTableFor.config.table_for_icon_font_base_class}-#{parsed_extras[:icon]}"></i>}
230
243
  end
231
244
  ancestors_list = parsed_extras[:ancestors].presence || ""
232
245
  ancestors = ancestors_list.split(",").map do |ancestor|
233
246
  record.send(ancestor)
234
247
  end
235
- custom_action_class = %Q{#{CtTableFor.table_for_default_action_base_class} #{parsed_extras[:class]}}
236
- link_to(label.html_safe,
237
- polymorphic_path([parsed_extras[:link], *ancestors, record]),
238
- class: custom_action_class,
239
- method: parsed_extras[:method],
240
- title: parsed_extras[:title])
248
+ custom_action_class = %Q{#{CtTableFor.config.table_for_default_action_base_class} #{parsed_extras[:class]}}
249
+ options= {
250
+ class: custom_action_class,
251
+ method: parsed_extras[:method],
252
+ title: parsed_extras[:title]
253
+ }
254
+ options[:target]= parsed_extras[:target] if parsed_extras[:target].present?
255
+ link_to(label.html_safe, polymorphic_path([parsed_extras[:link]&.to_sym, *ancestors, record]), options)
241
256
  end
242
257
 
243
258
  def uri?(string)
@@ -253,5 +268,11 @@ module CtTableFor
253
268
  def parse_extras(extras)
254
269
  Hash[extras.collect { |extra| [extra.split(":").first, extra.split(":").last] } ].with_indifferent_access
255
270
  end
271
+
272
+ def get_css_classes(record, options = {})
273
+ style = options[:style].try(:call, record) || ""
274
+ tr_class = options[:tr_class] || ""
275
+ "class='#{tr_class} #{style}'"
276
+ end
256
277
  end
257
278
  end
@@ -1,17 +1,18 @@
1
1
  module CtTableFor
2
- class << self
3
- mattr_accessor :table_for_default_class
4
- mattr_accessor :table_for_wrapper_default_class
5
- mattr_accessor :table_for_default_action_base_class
6
- mattr_accessor :table_for_action_class
7
- mattr_accessor :table_for_breakpoint
8
- mattr_accessor :table_for_icon_font_base_class
9
- mattr_accessor :table_for_action_icons
10
- mattr_accessor :table_for_numeric_percentage_precision
11
- mattr_accessor :table_for_cell_for_image_image_class
12
- mattr_accessor :table_for_truncate_length
13
- mattr_accessor :table_for_truncate_separator
14
- mattr_accessor :table_for_truncate_omission
2
+ class Config
3
+ self.mattr_accessor :table_for_default_class
4
+ self.mattr_accessor :table_for_wrapper_default_class
5
+ self.mattr_accessor :table_for_default_action_base_class
6
+ self.mattr_accessor :table_for_action_class
7
+ self.mattr_accessor :table_for_breakpoint
8
+ self.mattr_accessor :table_for_icon_font_base_class
9
+ self.mattr_accessor :table_for_action_icons
10
+ self.mattr_accessor :table_for_numeric_percentage_precision
11
+ self.mattr_accessor :table_for_cell_for_image_image_class
12
+ self.mattr_accessor :table_for_truncate_length
13
+ self.mattr_accessor :table_for_truncate_separator
14
+ self.mattr_accessor :table_for_truncate_omission
15
+ self.mattr_accessor :table_for_td_default_prefix_class
15
16
 
16
17
  self.table_for_wrapper_default_class = "table-responsive"
17
18
  self.table_for_default_class = "table table-striped table-bordered table-condensed table-hover"
@@ -25,11 +26,16 @@ module CtTableFor
25
26
  self.table_for_truncate_length = 50
26
27
  self.table_for_truncate_separator = " "
27
28
  self.table_for_truncate_omission = "..."
29
+ self.table_for_td_default_prefix_class = "td-item"
30
+ end
31
+
32
+ def self.config
33
+ @config||= Config.new
28
34
  end
29
35
 
30
36
  # this function maps the vars from your app into your engine
31
37
  def self.setup(&block)
32
- yield self
38
+ yield config
33
39
  end
34
40
 
35
41
  class Engine < ::Rails::Engine
@@ -1,3 +1,3 @@
1
1
  module CtTableFor
2
- VERSION = '1.0.2'
2
+ VERSION = '1.2.1'
3
3
  end
metadata CHANGED
@@ -1,36 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ct_table_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Agustí B.R.
8
8
  - Isaac Massot
9
9
  - Marc Reniu
10
+ - Oliver Valls
10
11
  autorequire:
11
12
  bindir: bin
12
13
  cert_chain: []
13
- date: 2019-07-31 00:00:00.000000000 Z
14
+ date: 2021-10-22 00:00:00.000000000 Z
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: rails
17
18
  requirement: !ruby/object:Gem::Requirement
18
19
  requirements:
19
- - - "~>"
20
+ - - ">="
20
21
  - !ruby/object:Gem::Version
21
- version: '5.0'
22
+ version: '6.1'
22
23
  type: :runtime
23
24
  prerelease: false
24
25
  version_requirements: !ruby/object:Gem::Requirement
25
26
  requirements:
26
- - - "~>"
27
+ - - ">="
27
28
  - !ruby/object:Gem::Version
28
- version: '5.0'
29
+ version: '6.1'
29
30
  description: table_for is a rails table builder given an ActiveRecord
30
31
  email:
31
- - agusti.br@coditramuntana.com
32
- - issac.mg@coditramuntana.com
33
- - marc.rs@coditramuntana.com
32
+ - info@coditramuntana.com
34
33
  executables: []
35
34
  extensions: []
36
35
  extra_rdoc_files: []
@@ -67,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
66
  - !ruby/object:Gem::Version
68
67
  version: '0'
69
68
  requirements: []
70
- rubygems_version: 3.0.4
69
+ rubygems_version: 3.1.2
71
70
  signing_key:
72
71
  specification_version: 4
73
72
  summary: Rails table builder that makes it easy to do responsive tables ActiveRecord