table_for_helper 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
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,76 @@
1
+ table_for helper
2
+ ================
3
+
4
+ A Rails helper for generating tables from resources collections that works like form_for helper.
5
+
6
+ ## Installation
7
+
8
+ Add it to your Gemfile:
9
+
10
+ gem 'table_for'
11
+
12
+ Run the following command to install it:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install table_for
19
+
20
+ ## Usage
21
+
22
+ <%= table_for RESOURCES [, TABLE_TAG_OPTIONS] [BLOCK] %>
23
+
24
+ In the BLOCK you can configurate columns and footer cells
25
+
26
+ <%= table_for @users do |t|
27
+ t.column :NAME [, TH_TAG_OPTIONS] [VALUE_BUILDER_BLOCK]
28
+ ...
29
+ t.footer :NAME [, TD_TAG_OPTIONS] [VALUE_BUILDER_BLOCK]
30
+ ...
31
+ %>
32
+
33
+ A VALUE_BUILDER_BLOCK must return a value for a cell.
34
+
35
+ For example, if you place this into a view:
36
+
37
+ <%= table_for @users, :class => 'table' do |t|
38
+ t.column :email
39
+ t.column Role, :class => 'span3' do |row|
40
+ row.role.title
41
+ end
42
+ t.column :created_at
43
+ t.column :view_action, '' do |row|
44
+ link_to 'view', user_path(row.id)
45
+ end
46
+ end %>
47
+
48
+ It's will render something like this:
49
+
50
+ <table class="table">
51
+ <thead>
52
+ <tr>
53
+ <th>Email</th>
54
+ <th class="span3">Role</th>
55
+ <th>Created at</th>
56
+ <th></th>
57
+ </tr>
58
+ </thead>
59
+ <tbody>
60
+ <tr>
61
+ <td>user@mail</td>
62
+ <td>Seller</td>
63
+ <td>12.10.12, 14:30</td>
64
+ <td><a href="/users/1">view</a></td>
65
+ </tr>
66
+ ...
67
+ </tbody>
68
+ </table>
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,100 @@
1
+ module TableFor
2
+ class Builder
3
+ include ActionView::Helpers::TagHelper
4
+
5
+ # Initialize variables
6
+ def initialize collection, options = nil
7
+ @collection = collection
8
+ @options = options.symbolize_keys
9
+ @model = collection.first.class
10
+
11
+ @head = {}
12
+ @body = {}
13
+ @footer = {}
14
+ @footer_options = {}
15
+
16
+ yield self if block_given?
17
+ end
18
+
19
+ # Add a new column to the table
20
+ def column name, title = nil, options = nil, &block
21
+ # Move a value from the title to options variable for 2 params usage
22
+ if title.is_a? Hash
23
+ options = title
24
+ title = nil
25
+ end
26
+
27
+ # Take a title for this column from the dictionary
28
+ if !title
29
+ # A title for a reference
30
+ if name.is_a? Class
31
+ title = name.model_name.human
32
+ # A title for an model attribute
33
+ else
34
+ title = @model.human_attribute_name(name)
35
+ end
36
+ end
37
+
38
+ # Create a html head cell
39
+ @head[name] = content_tag('th', title, options)
40
+
41
+ # Save a builder for rows
42
+ @body[name] = block if block_given?
43
+ end
44
+
45
+ # Add a footer row column value
46
+ def footer name, options = nil, &block
47
+ # Save a builder for a footer cell
48
+ @footer[name] = block if block_given?
49
+
50
+ # Save options for a footer cell
51
+ @footer_options[name] = options if options
52
+ end
53
+
54
+ # Generate a html
55
+ def to_html
56
+ # Add all model attributes if no block given
57
+ @model.attribute_names.each{|name| column(name)} if @head.empty?
58
+
59
+ # Save list of view attributes
60
+ columns = @head.keys
61
+
62
+ # Create a html row tag for head cells
63
+ head = content_tag('tr', @head.values.join.html_safe)
64
+
65
+ # Construct body html rows
66
+ body = ''
67
+ @collection.each do |resource|
68
+ row = ''
69
+ columns.each do |column|
70
+ if @body[column]
71
+ cell = @body[column].call(resource)
72
+ else
73
+ cell = resource[column]
74
+ end
75
+ row << content_tag('td', cell.to_s)
76
+ end
77
+ body << content_tag('tr', row.html_safe)
78
+ end
79
+
80
+ # Construct the footer row
81
+ footer = ''
82
+ if !@footer.empty?
83
+ columns.each do |column|
84
+ if @footer[column]
85
+ footer << content_tag('td', @footer[column].call(@collection), @footer_options[column])
86
+ else
87
+ footer << content_tag('td')
88
+ end
89
+ end
90
+ footer = content_tag('tr', footer.html_safe)
91
+ end
92
+
93
+ # Join all table parts, put it to a table tag and return
94
+ table = ''
95
+ table << content_tag('thead', head)
96
+ table << content_tag('tbody', body.html_safe + footer.html_safe)
97
+ content_tag('table', table.html_safe, @options)
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module TableFor
2
+ VERSION = "0.2"
3
+ end
data/lib/table_for.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "table_for/version"
2
+ require "table_for/builder"
3
+
4
+ module TableFor
5
+ # Creates a html table from a resource collection
6
+ def table_for(resources, options = {}, &block)
7
+ Builder.new(resources, options, &block).to_html
8
+ end
9
+ end
10
+
11
+ ActionController::Base.class_eval do
12
+ helper TableFor
13
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/table_for/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Nick Nizovtsev']
6
+ gem.email = ['nizovtsevnv@gmail.com']
7
+ gem.description = %q{A Rails helper for generating tables from resources collections.}
8
+ gem.summary = %q{Easily making tables from collections with a DSL.}
9
+ gem.homepage = 'http://github.com/nizovtsevnv/table_for_helper'
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "table_for_helper"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = TableFor::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_for_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nick Nizovtsev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A Rails helper for generating tables from resources collections.
15
+ email:
16
+ - nizovtsevnv@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/table_for.rb
27
+ - lib/table_for/builder.rb
28
+ - lib/table_for/version.rb
29
+ - table_for_helper.gemspec
30
+ homepage: http://github.com/nizovtsevnv/table_for_helper
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.24
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Easily making tables from collections with a DSL.
54
+ test_files: []