snaptable 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41a46aa96e307baf3ccdf299cdf0775f37d558e1
4
+ data.tar.gz: bac55a7791146152abeb42727d92c0287434f8e7
5
+ SHA512:
6
+ metadata.gz: dcca510c040d61d7dd672c93951ad9cc3198fb208d03c4d705d732ad50758615c87f3dd3dd01180be747851ad0fc227a918052bcfd89a58645af50aa6a7dff7b
7
+ data.tar.gz: 8456f737cdf4b70d0e7ae6b2f7adb7c7ab5bb7af6a87a309df92ca219b26ac4ac30e973e121f38a049d14b6de3fafcaea4d10c30d1c54f12be961bd948ad05e0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autotable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 khcr
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Snaptable
2
+
3
+ A gem that generate HTML tables from your models to display in your admin views. It supports pagination (carrierwave), sorting and searching. It is also possible to customize the tables.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'snaptable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install snaptable
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/khcr/snaptable/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,37 @@
1
+ /* Admin table */
2
+
3
+ $(document).on("ready page:load", function() {
4
+
5
+ var tableButtons = $('.table_buttons'),
6
+ editButton = tableButtons.find('a[class="edit"]'),
7
+ deleteButton = tableButtons.find('a[class="delete"]'),
8
+ path = window.location.pathname + '/';
9
+ table = $("#admin_table");
10
+
11
+ // add ajax to the pagination
12
+ table.on("click", ".pagination a", function() {
13
+ $.getScript(this.href);
14
+ return false;
15
+ });
16
+
17
+ // line clickable
18
+ table.on("click", "tbody tr", function(e) {
19
+ var id = $(this).data('url') ;
20
+ if ( typeof id !== 'undefined' && !$(this).hasClass('selected') ) {
21
+ $('tr.selected').removeClass('selected');
22
+ $(this).addClass('selected');
23
+ deleteButton.add(editButton).addClass("on");
24
+ editButton.attr('href', path + id + '/edit');
25
+ deleteButton.attr('href', path + id);
26
+ }
27
+ });
28
+
29
+ // Double click
30
+ table.on("dblclick", "tbody tr", function() {
31
+ var id = $(this).data('url');
32
+ if ( typeof id !== 'undefined' ) {
33
+ window.location = path + id + '/edit';
34
+ }
35
+ });
36
+
37
+ });
@@ -0,0 +1,88 @@
1
+ /* Table */
2
+
3
+ #admin_table {
4
+ width: 100%;
5
+ overflow: auto;
6
+
7
+ table {
8
+ border-collapse: collapse;
9
+ width: 100%;
10
+ max-width: 100%;
11
+ line-height: 21px;
12
+ margin: 57px 0 0 0;
13
+ thead {
14
+ tr {
15
+ th {
16
+ vertical-align: bottom;
17
+ border-bottom: 1px solid #DADADA;
18
+ padding: 10px 8px 5px 8px;
19
+ font-size: 13px;
20
+ text-align: left;
21
+ background: #3d3d3e;
22
+ color: #c0c0c0;
23
+ font-weight: 400;
24
+ &:not(:first-child) {
25
+ }
26
+ &:not(:last-child) {
27
+ }
28
+ a {
29
+ color:#c0c0c0;
30
+ font-weight: bold;
31
+ }
32
+ }
33
+ }
34
+ }
35
+ tbody {
36
+ td {
37
+ font-size: 12px;
38
+ padding: 7px 8px;
39
+ &:not(:first-child) {
40
+ border-left: 1px solid #DADADA;
41
+ }
42
+ }
43
+ }
44
+ tr.even {
45
+ background: #f8f8f8;
46
+ }
47
+ tr.selected {
48
+ background: #F0BEBE;
49
+ }
50
+ }
51
+
52
+ .none {
53
+ text-align: center;
54
+ }
55
+
56
+ .table_buttons {
57
+ text-align: right;
58
+ padding: 20px;
59
+ position: absolute;
60
+ }
61
+
62
+ .table_buttons a {
63
+ padding: 7px 10px;
64
+ border-radius: 3px;
65
+ color: #fff;
66
+ font-weight: lighter;
67
+ font-size: 14px;
68
+ }
69
+
70
+ .table_buttons a {
71
+ background: #aaa;
72
+ pointer-events: none;
73
+ cursor: default;
74
+ }
75
+
76
+ .table_buttons a.on {
77
+ background: #7f72f5;
78
+ pointer-events: auto;
79
+ cursor: auto;
80
+ }
81
+
82
+ .table_buttons a.add {
83
+ background: #7f72f5;
84
+ pointer-events: auto;
85
+ cursor: auto;
86
+ }
87
+
88
+ }
@@ -0,0 +1,5 @@
1
+ <div class="table_buttons">
2
+ <%= link_to "Ajouter", request.path + "/new", class: "add" %>
3
+ <a class="edit" href="#">Editer</a>
4
+ <a class="delete" href="#" data-method="delete" rel="nofollow" data-confirm="Etes-vous sûr de vouloir supprimer cette entrée ?">Supprimer</a>
5
+ </div>
@@ -0,0 +1,9 @@
1
+ <p>Rechercher</p>
2
+
3
+ <%= form_tag nil, method: :get, remote: true do %>
4
+
5
+ <%= search_field_tag :query, params[:query] %>
6
+
7
+ <%= submit_tag "Chercher" %>
8
+
9
+ <% end %>
@@ -0,0 +1,25 @@
1
+ <%= render 'search_field' if presenter.options[:search] == true %>
2
+ <%= render 'buttons' unless presenter.options[:buttons] == false %>
3
+ <table>
4
+ <thead>
5
+ <tr>
6
+ <% presenter.column_names.each do |column| %>
7
+ <th><%= presenter.sortable(column) %></th>
8
+ <% end %>
9
+ </tr>
10
+ </thead>
11
+ <tbody>
12
+ <% if presenter.records.any? %>
13
+ <% presenter.records.each do |record| %>
14
+ <tr class="<%= cycle('odd', 'even') %>" data-url="<%= presenter.url(record) %>">
15
+ <% presenter.values(record).each do |value| %>
16
+ <td><%= truncate((strip_tags value), length: 40) %></td>
17
+ <% end %>
18
+ </tr>
19
+ <% end %>
20
+ <% else %>
21
+ <tr><td class="none" colspan="<%= presenter.column_names.count %>">Il n'y a aucun élément à afficher.</td></tr>
22
+ <% end %>
23
+ </tbody>
24
+ </table>
25
+ <%= will_paginate presenter.records %>
@@ -0,0 +1 @@
1
+ $('#admin_table').html('<%= j @table.present %>') ;
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "snaptable"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,51 @@
1
+ require 'snaptable/constructor/sortable'
2
+ require 'snaptable/constructor/collection'
3
+ require 'snaptable/constructor/renderer'
4
+
5
+ module Snaptable
6
+ module Constructor
7
+
8
+ class BaseTable < SimpleDelegator
9
+ include Sortable
10
+ include Collection
11
+ include Renderer
12
+
13
+ def initialize(parent, collection = nil, options = {})
14
+ super(parent)
15
+ @collection = collection
16
+ @options = options
17
+ @token = stored_token
18
+ end
19
+
20
+ def options
21
+ @options
22
+ end
23
+
24
+ def column_names
25
+ model.column_names
26
+ end
27
+
28
+ def url(e)
29
+ e.id
30
+ end
31
+
32
+ def values(element)
33
+ attributes.map do |attribute|
34
+ if attribute.is_a? Symbol
35
+ element.send(attribute)
36
+ else
37
+ element.send(*attribute.keys).send(*attribute.values)
38
+ end.to_s
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def stored_token
45
+ session[:token] = params[:token] if params[:token]
46
+ return session[:token]
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,41 @@
1
+ module Snaptable
2
+ module Constructor
3
+ module Collection
4
+
5
+ def collection
6
+ @collection ||= model.includes(belongs_to_associations)
7
+ end
8
+
9
+ def records
10
+ @records ||= filter(collection).paginate(page: params[:page], per_page: 30).order(sort_column + " " + sort_direction)
11
+ end
12
+
13
+ def filter(collection)
14
+ if options[:search] == true
15
+ collection.joins(search_associations).where(query_fields, query: "%#{params[:query]}%", id: params[:query].to_i)
16
+ else
17
+ collection
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def query_fields
24
+ self.class::Search.fields.map do |key, values|
25
+ values.map do |value|
26
+ values.map{ |v| "#{key}.#{v} LIKE :query OR"}.join(" ")
27
+ end
28
+ end.join(" ") + " #{column_name('id')} = :id"
29
+ end
30
+
31
+ def search_associations
32
+ self.class::Search.associations
33
+ end
34
+
35
+ def belongs_to_associations
36
+ model.reflect_on_all_associations(:belongs_to).map{ |a| a.name }
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,18 @@
1
+ module Snaptable
2
+ module Constructor
3
+ module Renderer
4
+
5
+ def present
6
+ render_to_string('/base', layout: false, locals: { presenter: self }).html_safe
7
+ end
8
+
9
+ def respond
10
+ respond_to do |format|
11
+ format.html
12
+ format.js { render '/sort' }
13
+ end
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,43 @@
1
+ module Snaptable
2
+ module Constructor
3
+ module Sortable
4
+
5
+ def sortable(column)
6
+ if model.reflect_on_association(column.gsub /_id/, '').nil?
7
+ view_context.link_to({sort: column, direction: direction(column), query: params[:query], page: page}, {remote: true, class: css_class(column)}) do
8
+ model.human_attribute_name(column)
9
+ end
10
+ else
11
+ model.human_attribute_name(column)
12
+ end
13
+ end
14
+
15
+ def sort_column
16
+ column_names.include?(params[:sort]) ? column_name(params[:sort]) : column_name("id")
17
+ end
18
+
19
+ def sort_direction
20
+ %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
21
+ end
22
+
23
+ def page
24
+ params[:page] || 1
25
+ end
26
+
27
+ def css_class(column)
28
+ column_name(column) == sort_column ? "current #{sort_direction}" : nil
29
+ end
30
+
31
+ def direction(column)
32
+ column_name(column) == sort_column && sort_direction == "asc" ? "desc" : "asc"
33
+ end
34
+
35
+ private
36
+
37
+ def column_name(column)
38
+ "#{model.table_name}.#{column}"
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ require 'snaptable/constructor/base_table'
2
+
3
+ module Snaptable
4
+ module Helpers
5
+
6
+ class Table < Snaptable::Constructor::BaseTable
7
+
8
+ def initialize(parent, model, collection = nil, options = {})
9
+ super(parent, collection, options)
10
+ @model = model
11
+ end
12
+
13
+ def values(element)
14
+ element.attributes.map do |attr_name, attr_value|
15
+ if attr_value.is_a?(Date) || attr_value.is_a?(Time) || attr_value.is_a?(DateTime)
16
+ l(attr_value, format: :short)
17
+ else
18
+ attr_value
19
+ end.to_s
20
+ end
21
+ end
22
+
23
+ def model
24
+ @model
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ module Snaptable
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ initializer "Snaptable.assets.precompile" do |app|
5
+ app.config.assets.precompile +=%w(autotable.js autotable.css)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Snaptable
2
+ module Rails
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'snaptable/rails/engine'
2
+ require 'snaptable/rails/version'
3
+
4
+ module Snaptable
5
+ module Rails
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'snaptable/helpers/table'
2
+
3
+ module Snaptable
4
+ class Railtie < Rails::Railtie
5
+ initializer 'Snaptable.tables' do
6
+ ActionController::Base.send :include, Helpers
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Snaptable
2
+ VERSION = "0.2.0"
3
+ end
data/lib/snaptable.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'snaptable/version'
2
+ require 'snaptable/railtie' if defined?(Rails)
3
+ require 'snaptable/rails'
data/snaptable.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'snaptable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "snaptable"
8
+ spec.version = Snaptable::VERSION
9
+ spec.authors = ["khcr"]
10
+ spec.email = ["kocher.ke@gmail.com"]
11
+
12
+ spec.summary = "A gem that generate HTML tables from your models to display in your admin views."
13
+ spec.description = spec.summary
14
+ spec.homepage = "http://github.com/khcr/snaptable"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.8"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+
25
+ spec.add_dependency "railties", ">= 4.2.0"
26
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snaptable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - khcr
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: railties
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 4.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 4.2.0
55
+ description: A gem that generate HTML tables from your models to display in your admin
56
+ views.
57
+ email:
58
+ - kocher.ke@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - app/assets/javascripts/autotable.js
70
+ - app/assets/stylesheets/autotable.css.scss
71
+ - app/views/application/_buttons.html.erb
72
+ - app/views/application/_search_field.html.erb
73
+ - app/views/base.html.erb
74
+ - app/views/sort.js.erb
75
+ - bin/console
76
+ - bin/setup
77
+ - lib/snaptable.rb
78
+ - lib/snaptable/constructor/base_table.rb
79
+ - lib/snaptable/constructor/collection.rb
80
+ - lib/snaptable/constructor/renderer.rb
81
+ - lib/snaptable/constructor/sortable.rb
82
+ - lib/snaptable/helpers/table.rb
83
+ - lib/snaptable/rails.rb
84
+ - lib/snaptable/rails/engine.rb
85
+ - lib/snaptable/rails/version.rb
86
+ - lib/snaptable/railtie.rb
87
+ - lib/snaptable/version.rb
88
+ - snaptable.gemspec
89
+ homepage: http://github.com/khcr/snaptable
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A gem that generate HTML tables from your models to display in your admin
113
+ views.
114
+ test_files: []