nie_easy_table 0.0.1

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: 15b222978bd9da080ba6e9547d45e4f8e3b885a5
4
+ data.tar.gz: b692642b1c3729936dba968abb0846b60681c018
5
+ SHA512:
6
+ metadata.gz: 1a6a16fd141ce20c70896e356730fc56f8119377440c5ce793e55221222cb5c9fc41e1e28a82335fed8dc1ea6fcc1e354e3f13d77786bfea791c71c197527599
7
+ data.tar.gz: 74d4b4fd3e4c1413c80593c647384831e704b596798058aff15866573216a5f42b04b15483f32bcd90699b5f928296c778927acb305356e5137af2a255d41340
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ test/dummy/db/*.sqlite3
5
+ test/dummy/db/*.sqlite3-journal
6
+ test/dummy/log/*.log
7
+ test/dummy/tmp/
8
+ test/dummy/.sass-cache
9
+
10
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,27 @@
1
+ source "http://ruby.taobao.org"
2
+
3
+ # Declare your gem's dependencies in easy_table.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use debugger
14
+ # gem 'debugger'
15
+
16
+ group :test do
17
+ gem 'nokogiri'
18
+ gem 'pry'
19
+ gem 'pry-debugger'
20
+ gem 'ammeter'
21
+
22
+ #gem 'rspec-rails'
23
+ gem 'rspec'
24
+ gem 'slim-rails'
25
+ gem 'capybara'
26
+ gem 'hashie'
27
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ EasyTable
2
+ ===============
3
+
4
+ This project rocks and uses MIT-LICENSE.
5
+
6
+
7
+ TODO
8
+ ==============
9
+
10
+ - [ ] I18n header
11
+ - [ ] custom header using options
12
+ - [x] guess model_class from current controller
13
+ - [x] guess model_class from current controller, with namespace
14
+ - [x] custom column using block
15
+ - [ ] custom column using class
16
+ - [x] add action column
17
+ - [x] specify multiple columns using :tds
18
+ - [x] config for action_column
19
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+
9
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
10
+ spec.pattern = 'spec/**/*_spec.rb'
11
+ spec.rcov = true
12
+ end
13
+
14
+ task :spec
15
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ # Maintain your gem's version:
6
+ require "easy_table/version"
7
+
8
+ # Describe your gem and declare its dependencies:
9
+ Gem::Specification.new do |s|
10
+ s.name = "nie_easy_table"
11
+ s.version = EasyTable::VERSION
12
+ s.authors = ["Dianhui Nie"]
13
+ s.email = ["niedhui@gmail.com"]
14
+ s.homepage = "http://github.com/niedhui/easy_table"
15
+ s.summary = "Create Table easily"
16
+ s.description = "Create Table easily for Rails app"
17
+ s.license = "MIT"
18
+
19
+ s.files = `git ls-files`.split($/)
20
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
22
+ s.require_paths = ["lib"]
23
+
24
+
25
+ s.add_dependency "rails", "> 3.0.0"
26
+
27
+ s.add_development_dependency "sqlite3"
28
+ end
data/lib/easy_table.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'action_view'
2
+ require 'active_support/core_ext'
3
+ require 'rails/generators'
4
+ module EasyTable
5
+ autoload :Config, 'easy_table/config'
6
+ autoload :ActionViewExtension, 'easy_table/action_view_extension'
7
+ autoload :Builder, 'easy_table/builder'
8
+ autoload :Base, 'easy_table/base'
9
+ autoload :Column, 'easy_table/column'
10
+ autoload :ActionColumn , 'easy_table/action_column'
11
+
12
+ autoload :Presenter, 'easy_table/presenter'
13
+
14
+ def self.config
15
+ EasyTable::Config
16
+ end
17
+ end
18
+
19
+ require 'easy_table/engine'
@@ -0,0 +1,17 @@
1
+ module EasyTable
2
+ class ActionColumn < Column
3
+
4
+ def header(table, view)
5
+ ""
6
+ end
7
+
8
+ def th_html
9
+ Config.action_th_html
10
+ end
11
+
12
+ def td_html
13
+ Config.action_td_html
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ module EasyTable
2
+ module ActionViewExtension
3
+ def easy_table_for(objects, options = {})
4
+ options[:model_class] ||= guess_model_class
5
+
6
+ EasyTable::Builder.build(objects, self, options) do |table|
7
+ yield table if block_given?
8
+ end.to_s
9
+ end
10
+
11
+ # TODO: support namespace
12
+ def guess_model_class
13
+ controller_name.classify.constantize
14
+ rescue
15
+ nil
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,46 @@
1
+ module EasyTable
2
+ class Base
3
+ attr_reader :columns, :items, :options, :model_class, :action_columns
4
+ attr_accessor :presenter
5
+
6
+ delegate :render, to: :presenter
7
+
8
+ # options:
9
+ # model_class: the class of the model, used for i18n, using with the human_attribute_name
10
+ def initialize(items, options = {})
11
+ @items = items
12
+ @options = options
13
+ @columns = []
14
+ @action_columns = []
15
+ process_options
16
+ end
17
+
18
+ def tds(*names)
19
+ names.each { |name| td(name) }
20
+ end
21
+
22
+ def td(name, options = {}, &block)
23
+ @columns << Column.new(name, options, &block)
24
+ end
25
+
26
+ def action(&block)
27
+ @action_columns << ActionColumn.new("", {}, &block)
28
+ end
29
+
30
+ def all_columns
31
+ @all_columns ||= (columns + action_columns)
32
+ end
33
+
34
+ def process_options
35
+ @model_class = options[:model_class] || NilModelClass.instance
36
+ end
37
+
38
+ class NilModelClass
39
+ include Singleton
40
+ def human_attribute_name(attribute)
41
+ attribute.to_s
42
+ end
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,10 @@
1
+ module EasyTable
2
+ class Builder
3
+ def self.build(items, view, options = {})
4
+ table = Base.new(items, options)
5
+ yield table if block_given?
6
+ table.presenter = Presenter.new(table, view)
7
+ table.render
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,34 @@
1
+ module EasyTable
2
+ class Column
3
+ attr_reader :name, :options, :value_block
4
+
5
+ def initialize(name, options = {}, &block)
6
+ @name, @options, @value_block = name, options, block
7
+ end
8
+
9
+ def header(table, view)
10
+ if options[:header].present?
11
+ options[:header]
12
+ elsif table.model_class
13
+ table.model_class.human_attribute_name(name)
14
+ end
15
+ end
16
+
17
+ def th_html
18
+ Config.th_html
19
+ end
20
+
21
+ def td_html
22
+ Config.td_html
23
+ end
24
+
25
+ def value(item, view)
26
+ if value_block
27
+ view.capture(item, &value_block)
28
+ else
29
+ item.send(name)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ module EasyTable
2
+ class Config
3
+ include ActiveSupport::Configurable
4
+
5
+ config_accessor :table_html, :tr_html, :th_html, :td_html, :action_th_html, :action_td_html
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails/engine'
2
+ module EasyTable
3
+ class Engine < ::Rails::Engine
4
+ initializer 'easy_table.initialize' do
5
+ ActiveSupport.on_load(:action_view) do
6
+ include EasyTable::ActionViewExtension
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,47 @@
1
+ module EasyTable
2
+ class Presenter
3
+ attr_reader :view, :table
4
+ delegate :content_tag, to: :view
5
+
6
+ def initialize(table, view)
7
+ @table, @view = table, view
8
+ end
9
+
10
+ def render
11
+ content_tag("table", Config.table_html) do
12
+ trs = "".html_safe
13
+ trs += render_header
14
+ trs += render_body
15
+ end
16
+ end
17
+
18
+ def render_header
19
+ content_tag("thead") do
20
+ content_tag("tr") do
21
+ ths = "".html_safe
22
+ table.all_columns.each do |column|
23
+ ths += content_tag("th", column.header(table, view), column.th_html)
24
+ end
25
+ ths
26
+ end
27
+ end
28
+ end
29
+
30
+ def render_body
31
+ content_tag("tbody") do
32
+ tbodys = "".html_safe
33
+ table.items.each do |item|
34
+ tbodys += content_tag("tr") do
35
+ tds = "".html_safe
36
+ table.all_columns.each do |column|
37
+ tds += content_tag("td", column.value(item, view), column.td_html)
38
+ end
39
+ tds
40
+ end
41
+ end
42
+ tbodys
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module EasyTable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,12 @@
1
+ module EasyTable
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+ desc "Create a easy_table initializer"
6
+
7
+ def copy_initializer
8
+ template "easy_table.rb", "config/initializers/easy_table.rb"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ EasyTable::Config.configure do |config|
2
+ config.table_html = { class: 'easy-table table table-bordered' }
3
+
4
+ config.action_td_html = { class: 'action'}
5
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :easy_table do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ require 'rails/version'
3
+ require 'generators/easy_table/install_generator'
4
+ require 'ammeter/init'
5
+ describe EasyTable::Generators::InstallGenerator do
6
+
7
+ before { run_generator }
8
+
9
+ it "generate config/initializers/easy_table.rb" do
10
+ File.read(file('config/initializers/easy_table.rb')).should =~ /EasyTable::Config.configure/
11
+ end
12
+
13
+ end
@@ -0,0 +1,14 @@
1
+ require "spec_helper"
2
+
3
+ module EasyTable
4
+ describe ActionViewExtension do
5
+ subject { ActionView::Base.new}
6
+
7
+ it { should respond_to :easy_table_for }
8
+
9
+ it " can render a table" do
10
+ subject.easy_table_for([]).should have_tag('table')
11
+ end
12
+ end
13
+
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ module EasyTable
3
+ describe Base do
4
+ subject { Base.new([]) }
5
+
6
+ context "tds" do
7
+ before { subject.tds :id, :name, :age}
8
+
9
+ it { should have(3).columns}
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+ module EasyTable
3
+ describe Builder do
4
+ let(:view) { ActionView::Base.new }
5
+ let(:users) { [DummyUser.new(1,'jack')]}
6
+
7
+ subject do
8
+ view.easy_table_for(users) do |table|
9
+ table.td :id
10
+ table.td :name
11
+ table.action { |model| view.link_to("View #{model.name}", "")}
12
+ end
13
+ end
14
+
15
+ context "table header" do
16
+ it { should have_xpath("//table/thead/tr/th[text()='id']")}
17
+ it { should have_xpath("//table/thead/tr/th[text()='name']")}
18
+ end
19
+
20
+ context "table body" do
21
+ it { should have(2).trs}
22
+ it { should have_xpath("//table/tbody/tr/td[text()='1']")}
23
+ it { should have_xpath("//table/tbody/tr/td[text()='jack']")}
24
+ end
25
+
26
+ context "table config" do
27
+ it { should have_tag("table.easy-table")}
28
+ it { should have_tag("th.action")}
29
+ end
30
+
31
+ context "no config for table" do
32
+ it "should have no class" do
33
+ Config.table_html = nil
34
+ Nokogiri::HTML(subject).at_css('table').attr(:class).should be_blank
35
+ end
36
+ end
37
+
38
+ context "i18n" do
39
+
40
+
41
+ context "table header" do
42
+ subject do
43
+ view.easy_table_for(users, model_class: DummyUser) do |table|
44
+ table.td :id
45
+ table.td :name
46
+ end
47
+ end
48
+ it { should have_xpath("//table/thead/tr/th[text()='ID']")}
49
+ it { should have_xpath("//table/thead/tr/th[text()='名称']")}
50
+ end
51
+
52
+ context " guess model class using controller_name" do
53
+ before { view.stub(:controller_name).and_return('dummy_users') }
54
+
55
+ it { should have_xpath("//table/thead/tr/th[text()='ID']")}
56
+ it { should have_xpath("//table/thead/tr/th[text()='名称']")}
57
+
58
+ end
59
+ end
60
+
61
+ context " custom column using block" do
62
+ subject do
63
+ view.easy_table_for(users) do |table|
64
+ table.td(:name) {|model| "Hi #{model.name}" }
65
+ end
66
+ end
67
+ it { should have_xpath("//table/tbody/tr/td[text()='Hi jack']")}
68
+ end
69
+
70
+ context " action column" do
71
+ it { should have_xpath("//table/tbody/tr/td/a[text()='View jack']")}
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ module EasyTable
4
+ describe Column do
5
+ let(:table) { Base.new([]) }
6
+ context "header" do
7
+ it "should ok if model_class is not exist" do
8
+ table = Base.new([])
9
+ column = Column.new(:name)
10
+ column.header(table, nil).should == 'name'
11
+ end
12
+
13
+ it "should using human attribute name" do
14
+ table = Base.new([], model_class: DummyUser)
15
+ column = Column.new(:name)
16
+ column.header(table, nil).should == '名称'
17
+ end
18
+
19
+ it "should using options header first" do
20
+ column = Column.new(:name, header: 'Name')
21
+ column.header(table, nil).should == 'Name'
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,35 @@
1
+ require 'easy_table'
2
+ require 'hashie'
3
+ #ENV["RAILS_ENV"] = "test"
4
+
5
+ #require File.expand_path("../dummy/config/environment.rb", __FILE__)
6
+ #require 'rspec/rails'
7
+ #require 'capybara/rspec'
8
+ Dir['./spec/support/**/*.rb'].each {|f| require f }
9
+ #Rails.backtrace_cleaner.remove_silencers!
10
+ #Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
11
+ # This file was generated by the `rspec --init` command. Conventionally, all
12
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
13
+ # Require this file using `require "spec_helper"` to ensure that it is only
14
+ # loaded once.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ config.treat_symbols_as_metadata_keys_with_true_values = true
19
+ config.run_all_when_everything_filtered = true
20
+ config.filter_run :focus
21
+
22
+ config.before :each do
23
+ EasyTable::Engine.initializers.each(&:run)
24
+ EasyTable::Config.configure do |t_config|
25
+ t_config.table_html = { class: 'easy-table table table-bordered' }
26
+ t_config.action_th_html = { class: 'action' }
27
+ end
28
+ end
29
+
30
+ # Run specs in random order to surface order dependencies. If you find an
31
+ # order dependency and want to debug it, you can fix the order by providing
32
+ # the seed, which is printed after each run.
33
+ # --seed 1234
34
+ config.order = 'random'
35
+ end
@@ -0,0 +1,7 @@
1
+ DummyUser = Struct.new(:id, :name) do
2
+
3
+ def self.human_attribute_name(attr)
4
+ {id: 'ID', name: '名称' }[attr]
5
+ end
6
+
7
+ end
@@ -0,0 +1,17 @@
1
+ require "nokogiri"
2
+
3
+
4
+ RSpec::Matchers.define :have_xpath do |xpath|
5
+ match { |string| !!Nokogiri::HTML(string.to_s).at_xpath(xpath) }
6
+ end
7
+
8
+ RSpec::Matchers.define :have_tag do |tag|
9
+ match { |string| !!Nokogiri::HTML(string.to_s).at_css(tag) }
10
+ end
11
+
12
+ class String
13
+ def trs
14
+ Nokogiri::HTML(self).css('tr')
15
+ end
16
+ end
17
+
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nie_easy_table
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dianhui Nie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>'
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>'
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Create Table easily for Rails app
42
+ email:
43
+ - niedhui@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - MIT-LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - easy_table.gemspec
56
+ - lib/easy_table.rb
57
+ - lib/easy_table/action_column.rb
58
+ - lib/easy_table/action_view_extension.rb
59
+ - lib/easy_table/base.rb
60
+ - lib/easy_table/builder.rb
61
+ - lib/easy_table/column.rb
62
+ - lib/easy_table/config.rb
63
+ - lib/easy_table/engine.rb
64
+ - lib/easy_table/presenter.rb
65
+ - lib/easy_table/version.rb
66
+ - lib/generators/easy_table/install_generator.rb
67
+ - lib/generators/templates/easy_table.rb
68
+ - lib/tasks/easy_table_tasks.rake
69
+ - spec/generators/install_generator_spec.rb
70
+ - spec/lib/action_view_extension_spec.rb
71
+ - spec/lib/base_spec.rb
72
+ - spec/lib/builder_spec.rb
73
+ - spec/lib/column_spec.rb
74
+ - spec/spec_helper.rb
75
+ - spec/support/dummy_models.rb
76
+ - spec/support/matchers.rb
77
+ homepage: http://github.com/niedhui/easy_table
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Create Table easily
101
+ test_files:
102
+ - spec/generators/install_generator_spec.rb
103
+ - spec/lib/action_view_extension_spec.rb
104
+ - spec/lib/base_spec.rb
105
+ - spec/lib/builder_spec.rb
106
+ - spec/lib/column_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/support/dummy_models.rb
109
+ - spec/support/matchers.rb