ct_table_for 0.1.8.beta

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fc412af2e425565111e6c532d02cfe307fd86b8e
4
+ data.tar.gz: 3d6b2efb3cc3c2f2722a4f23849523be0bdc14e6
5
+ SHA512:
6
+ metadata.gz: fe0dbd42da97ea7d3f26d265dd8bb0b29b41388b717077358634ff180cc365e0618caf25a6fe7b0e3674a41df643fa248b1e7cb86e8dd590bad0c2b75f424b6e
7
+ data.tar.gz: de7598062867f6ee45eff7b754f1feb7708f953087603a41d15c731b6659609bf6de12446085fa17755591ab6631606183f65cfeb44e4824cf85d9f609db7f32
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 CodiTramuntana
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # Rails Responsive Table
2
+
3
+ **[Rails Responsive Table](https://github.com/CodiTramuntana/ct_table_for)** is a rails table builder for an ActiveRecord collection.
4
+
5
+ ## Optional gems
6
+
7
+ * [Bootstrap](http://getbootstrap.com/)
8
+ * [Font Awesome](http://fontawesome.io/)
9
+ * [Ransack](https://github.com/activerecord-hackery/ransack)
10
+ * [Paperclip](https://github.com/thoughtbot/paperclip)
11
+ * [CanCanCan](https://github.com/CanCanCommunity/cancancan)
12
+
13
+ ## Installation
14
+
15
+ Add it to your Gemfile:
16
+
17
+ `gem 'ct_table_for', '~> 0.1.8'`
18
+
19
+ Then:
20
+
21
+ `bundle`
22
+
23
+ Then require the CSS in your `application.css` file:
24
+
25
+ ```css
26
+ /*
27
+ *= require table_for
28
+ */
29
+ ```
30
+
31
+ or in sass
32
+
33
+ ```sass
34
+ @import "table_for"
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ To get started, just use the `table_for_for` helper. Here's an example:
40
+
41
+ ```erb
42
+ <%= table_for Model, @collection %>
43
+ ```
44
+
45
+ ### Customizing
46
+
47
+ Create config variables in your app's /config/initializers/ct_table_for.rb
48
+
49
+ ```ruby
50
+ CtTableFor.setup do |config|
51
+ config.table_for_wrapper_default_class = "table-responsive"
52
+ config.table_for_default_class = "table table-striped table-bordered table-condensed table-hover"
53
+ config.table_for_breakpoint = "992px" # or could be done by sass
54
+ config.table_for_icon_font = "fa"
55
+ config.table_for_action_icons = {show: "eye", edit: "pencil", destroy: "trash"}
56
+ end
57
+ ```
58
+ You can also define the breakpoint in your `sass` before importing `table_for`:
59
+
60
+ ```sass
61
+ $table-for-breakpoint: 768px;
62
+ @import "table_for"
63
+ ```
64
+
65
+
66
+ ## Development
67
+
68
+ To develop the Gem, clone this repo and in your Rails Test application edit the `Gemfile` and edit the path to your local repo:
69
+
70
+ ```
71
+ gem 'table_for', path: '/home/user/path/to/table_for'
72
+ ```
73
+
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
78
+
79
+ ## License
80
+
81
+ `table_for` is Copyright © 2017 CodiTramuntana SL. It is free software, and may be redistributed under the terms specified in the LICENSE file.
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
84
+
85
+ # About CodiTramuntana
86
+
87
+ ![CodiTramuntana's Logo](https://avatars0.githubusercontent.com/u/27996979?v=3&u=b0256e23ae7b2f237e3d1b5f2b2abdfe3092b24c&s=400)
88
+
89
+ Maintained by [CodiTramuntana](http://www.coditramuntana.com).
90
+
91
+ The names and logos for CodiTramuntana are trademarks of CodiTramuntana SL.
92
+
93
+ We love open source software!
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'CtTableFor'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+ load 'rails/tasks/statistics.rake'
20
+
21
+
22
+
23
+ require 'bundler/gem_tasks'
24
+
@@ -0,0 +1,39 @@
1
+ $table-for-breakpoint: <%= CtTableFor.table_for_breakpoint %> !default;
2
+
3
+ @media (max-width: $table-for-breakpoint) {
4
+ // table as cards
5
+ .table-for {
6
+ thead {
7
+ display: none;
8
+ visibility: hidden;
9
+ }
10
+ tbody, tr, th, td {
11
+ border: 0;
12
+ display: block;
13
+ padding: 0;
14
+ text-align: left;
15
+ white-space: normal;
16
+ }
17
+
18
+ // give each row a little space
19
+ tr {
20
+ padding: .5rem;
21
+ }
22
+ th[data-title]:before,
23
+ td[data-title]:before {
24
+ content: attr(data-title) ":\00A0";
25
+ font-weight: bold;
26
+ }
27
+ th:not([data-title]) {
28
+ font-weight: bold;
29
+ }
30
+
31
+ // hide empty cells
32
+ td:empty {
33
+ display: none;
34
+ }
35
+ &.table-bordered > tbody > tr > td {
36
+ border-color: transparent;
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,186 @@
1
+ module CtTableFor
2
+ module ApplicationHelper
3
+ require 'uri'
4
+
5
+ ####################################################################################
6
+ # RWD Table
7
+ # use as: table_for Model, @collection, options: {}
8
+ # options: {
9
+ # actions: {
10
+ # buttons: %w(show, edit)}, // Hash: with array of buttons for actions
11
+ # premodel: [:bo, :admin], // Array: of symbols for nested namespaces/models
12
+ # icons: true // Boolean: if true show actions as icons
13
+ # }
14
+ # attributes: %(name:sortable email), // Array: of model attibutes to show in table
15
+ # // with extra parameter `:` add cell options (sort, image size)
16
+ # // if the attribute is a relation pass the attribute to show
17
+ # // if the attribute has an association will show a number with the count
18
+ # id: "my-id", // String: adds custom id to <table> element
19
+ # class: "my-custom-css", // String: add custom class to <table> element
20
+ # tr_class: "my-custom-css" // String: add custom class to <tr> element
21
+ #}
22
+ ####################################################################################
23
+
24
+ def table_for model, collection, options: {}
25
+ custom_id = options[:id].present? ? %Q{id="#{options[:id]}"} : ""
26
+ html = %Q{<div class="table-for-wrapper #{CtTableFor.table_for_wrapper_default_class}">}
27
+ html << %Q{<table #{custom_id} class="table-for #{CtTableFor.table_for_default_class} #{options[:class]}">}
28
+ html << table_for_header(model, has_actions: options[:actions].present?, options: options)
29
+ html << table_for_content(model, collection, options: options)
30
+ html << %Q{</table>}
31
+ html << %Q{</div>}
32
+ html.html_safe
33
+ end
34
+
35
+ def table_for_attributes model, options
36
+ options[:attributes] || model.attribute_names
37
+ end
38
+
39
+ def table_for_header model, has_actions: false, options: {}
40
+ html = ""
41
+ html << %Q{<thead>}
42
+ html << %Q{<tr>}
43
+ table_for_attributes(model, options).each do |attribute|
44
+ html << %Q{<th>}
45
+ attribute, *params = attribute.split(":")
46
+ html << if defined?(Ransack) and params.include? "sortable"
47
+ sort_link(@q, attribute, I18n.t("#{attribute}", scope: [:activerecord, :attributes, model.to_s.underscore]).capitalize )
48
+ else
49
+ model.human_attribute_name("#{attribute}").capitalize
50
+ end
51
+ html << %Q{</th>}
52
+ end
53
+ html << %Q{<th class="actions">#{I18n.t(:actions, scope: [:table_for]).capitalize}</th>} if has_actions
54
+ html << %Q{</tr>}
55
+ html << %Q{</thead>}
56
+ html.html_safe
57
+ end
58
+
59
+ def table_for_content model, collection, options: {}
60
+ html = ""
61
+ html << %Q{<tbody>}
62
+ if collection.present?
63
+ custom_tr_class = options[:tr_class].present? ? %Q{class="#{options[:tr_class]}"} : ""
64
+ collection.each do |record|
65
+ html << %Q{<tr data-colection-id="#{record.try(:id)}" #{custom_tr_class}>}
66
+ table_for_attributes(model, options).each do |attribute|
67
+ attribute, *params = attribute.split(":")
68
+ html << table_for_cell( model, record, attribute, cell_options: params )
69
+ end
70
+ html << table_for_actions( record, options: options) if options[:actions].present?
71
+ html << %Q{</tr>}
72
+ end
73
+ else
74
+ html << %Q{<tr>}
75
+ html << %Q{<td colspan=#{options[:attributes].size + 1}>}
76
+ html << I18n.t("table_for.messages.no_records")
77
+ html << %Q{</td>}
78
+ html << %Q{</tr>}
79
+ end
80
+ html << %Q{</tbody>}
81
+ html.html_safe
82
+ end
83
+
84
+ def table_for_cell model, record, attribute, cell_options: {}
85
+ html = ""
86
+ value = record.try(attribute.to_sym)
87
+
88
+ html << %Q{<td data-title="#{model.human_attribute_name("#{attribute}").capitalize}">}
89
+ case value
90
+ when NilClass
91
+ html << %Q{<i class="fa fa-asterisk text-muted"></i>}
92
+ when TrueClass, FalseClass
93
+ html << %Q{<i class="fa #{value ? "fa-check text-success" : "fa-times text-danger"}"></i>}
94
+ when Numeric
95
+ if cell_options.include? "currency"
96
+ html << number_to_currency(value)
97
+ else
98
+ html << %Q{<code>#{value}</code>}
99
+ end
100
+ when ActiveSupport::TimeWithZone
101
+ # TODO: value.in_time_zone
102
+ html << %Q{<code>#{value.strftime("%d/%m/%Y %H:%M:%S")}</code>}
103
+ when Time
104
+ # TODO: value.in_time_zone
105
+ html << %Q{<code>#{value.strftime("%H:%M:%S")}</code>}
106
+ when ActiveRecord::Base
107
+ if cell_options.present?
108
+ html << %Q{#{value.send cell_options[0]}}
109
+ else
110
+ html << %{#{(value.try(:title) || value.try(:name))}}
111
+ end
112
+ when ActiveRecord::Associations::CollectionProxy
113
+ html << %Q{#{value.count}}
114
+ else
115
+ if uri?(value)
116
+ html << link_to(value, value)
117
+ elsif defined?(PaperClip) and record.is_a?(PaperClip::Attachment)
118
+ html << table_for_cell_for_image( record, attribute, cell_options: cell_options )
119
+ else
120
+ html << value.to_s.truncate(50, separator: " ")
121
+ end
122
+ end
123
+ html << %Q{</td>}
124
+ html.html_safe
125
+ end
126
+
127
+ def table_for_cell_for_image record, attribute, cell_options: {}
128
+ html = ""
129
+ size = cell_options.select{ |opt| ["thumb", "original", "small", "medium"].include? opt }.first || "thumb"
130
+
131
+ html << image_tag(record.send(attribute).url(size), class: 'img-fluid', style: "max-height: 100px;")
132
+ html.html_safe
133
+ end
134
+
135
+
136
+ def table_for_actions(record, options: {} )
137
+ return "" if options[:actions].blank?
138
+ html = ""
139
+ html << %Q{<td>}
140
+ html << %Q{<div class="btn-group btn-group-sm" role="group" aria-label="#{I18n.t(:actions, scope: [:table_for]).capitalize}">}
141
+ nesting = (options[:actions][:premodel] || []) + [record]
142
+ buttons, *btn_options = options[:actions][:buttons].split(":")
143
+ buttons.each do |action|
144
+ return "" if defined?(CanCanCan) and cannot?(action, record)
145
+ label = I18n.t(action.to_sym, scope: [:table_for, :buttons]).capitalize
146
+ case action.to_sym
147
+ when :show
148
+ if options[:actions][:icons] != false
149
+ label = %Q{<i class="#{CtTableFor.table_for_icon_font_base_class} #{CtTableFor.table_for_icon_font_base_class}-#{CtTableFor.table_for_action_icons[:show]}"></i>}
150
+ end
151
+ html << link_to(label.html_safe, polymorphic_path(nesting), class: "btn btn-primary btn-sm")
152
+ when :edit
153
+ if options[:actions][:icons] != false
154
+ label = %Q{<i class="#{CtTableFor.table_for_icon_font_base_class} #{CtTableFor.table_for_icon_font_base_class}-#{CtTableFor.table_for_action_icons[:edit]}"></i>}
155
+ end
156
+ html << link_to(label.html_safe, edit_polymorphic_path(nesting), class: "btn btn-success btn-sm")
157
+ when :destroy
158
+ if options[:actions][:icons] != false
159
+ label = %Q{<i class="#{CtTableFor.table_for_icon_font_base_class} #{CtTableFor.table_for_icon_font_base_class}-#{CtTableFor.table_for_action_icons[:destroy]}"></i>}
160
+ end
161
+ html << link_to(label.html_safe, polymorphic_path(nesting),
162
+ method: :delete, class: "btn btn-danger btn-sm",
163
+ data: { confirm: I18n.t('are_you_sure').capitalize })
164
+ else
165
+ # TODO:
166
+ # nesting_custom = nesting + btn_options[0]
167
+ # label = icon CtTableFor.table_for_action_icons[:custom] if options[:actions][:icons] != false and defined?(FontAwesome)
168
+ # html << link_to(label, polymorphic_path(nesting_custom), class: "btn btn-default btn-sm")
169
+ end
170
+ end
171
+ html << %Q{</div>}
172
+ html << %Q{</td>}
173
+ html.html_safe
174
+ end
175
+
176
+ def uri?(string)
177
+ # http://ruby-doc.org/stdlib-2.4.0/libdoc/uri/rdoc/URI.html
178
+ uri = URI.parse(string)
179
+ %w( http https ).include?(uri.scheme)
180
+ rescue URI::BadURIError
181
+ false
182
+ rescue URI::InvalidURIError
183
+ false
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,15 @@
1
+ ca:
2
+ table_for:
3
+ actions: Accions
4
+ buttons:
5
+ back: Enrera
6
+ create: Crear
7
+ delete: :'table_for.buttons.destroy'
8
+ destroy: Eliminar
9
+ edit: Editar
10
+ new: Nou
11
+ show: Mostrar
12
+ update: Actualitzar
13
+ messages:
14
+ are_you_sure: Està segur?
15
+ no_records: No hi han registres
@@ -0,0 +1,15 @@
1
+ en:
2
+ table_for:
3
+ actions: Actions
4
+ buttons:
5
+ back: Back
6
+ create: Create
7
+ delete: :'table_for.buttons.destroy'
8
+ destroy: Delete
9
+ edit: Edit
10
+ new: New
11
+ show: Show
12
+ update: Update
13
+ messages:
14
+ are_you_sure: Are you sure?
15
+ no_records: No records found
@@ -0,0 +1,15 @@
1
+ es:
2
+ table_for:
3
+ actions: Acciones
4
+ buttons:
5
+ back: Atrás
6
+ create: Crear
7
+ delete: :'table_for.buttons.destroy'
8
+ destroy: Eliminar
9
+ edit: Editar
10
+ new: Nuevo
11
+ show: Ver
12
+ update: Actualiza
13
+ messages:
14
+ are_you_sure: ¿Está seguro?
15
+ no_records: No hay registros
@@ -0,0 +1,30 @@
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_breakpoint
6
+ mattr_accessor :table_for_icon_font_base_class
7
+ mattr_accessor :table_for_action_icons
8
+
9
+ self.table_for_wrapper_default_class = "table-responsive"
10
+ self.table_for_default_class = "table table-striped table-bordered table-condensed table-hover"
11
+ self.table_for_breakpoint = "992px"
12
+ self.table_for_icon_font_base_class = "fa"
13
+ self.table_for_action_icons = {show: "eye", edit: "pencil", destroy: "trash", custom: "gear"}
14
+
15
+ end
16
+
17
+ # this function maps the vars from your app into your engine
18
+ def self.setup(&block)
19
+ yield self
20
+ end
21
+
22
+ class Engine < ::Rails::Engine
23
+ isolate_namespace CtTableFor
24
+ paths["app"]
25
+
26
+ config.to_prepare do
27
+ ApplicationController.helper(CtTableFor::ApplicationHelper)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module CtTableFor
2
+ VERSION = '0.1.8.beta'
3
+ end
@@ -0,0 +1,5 @@
1
+ require "ct_table_for/engine"
2
+
3
+ module CtTableFor
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :ct_table_for do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ct_table_for
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.8.beta
5
+ platform: ruby
6
+ authors:
7
+ - Agustí B.R.
8
+ - Isaac Massot
9
+ - Marc Reniu
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2017-05-10 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '5.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '5.0'
29
+ description: table_for is a rails table builder given an ActiveRecord
30
+ email:
31
+ - agusti.br@coditramuntana.com
32
+ - issac.mg@coditramuntana.com
33
+ - marc.rs@coditramuntana.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - LICENSE
39
+ - README.md
40
+ - Rakefile
41
+ - app/assets/stylesheets/table_for.scss.erb
42
+ - app/helpers/ct_table_for/application_helper.rb
43
+ - config/locales/ca.yml
44
+ - config/locales/en.yml
45
+ - config/locales/es.yml
46
+ - lib/ct_table_for.rb
47
+ - lib/ct_table_for/engine.rb
48
+ - lib/ct_table_for/version.rb
49
+ - lib/tasks/ct_table_for_tasks.rake
50
+ homepage: https://github.com/CodiTramuntana/ct_table_for
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">"
66
+ - !ruby/object:Gem::Version
67
+ version: 1.3.1
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.5.1
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Rails table builder that makes it easy to do responsive tables ActiveRecord
74
+ test_files: []