grid_fu 0.0.1

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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slashadmin-table_for.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Victor Sokolov
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,47 @@
1
+ # GridFu
2
+
3
+ https://github.com/evilmartians/slashadmin/issues/3
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'grid_fu'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install grid_fu
18
+
19
+ ## Usage
20
+
21
+ Do not forget to describe config options.
22
+
23
+ ## TODO
24
+
25
+ 1. Default header & footer if none present.
26
+ 2. Render body, footer and header separately
27
+ 3. Specs.
28
+ 4. Sort?
29
+ 5. Nice output.
30
+ 6. Default data attrs for everything.
31
+ 7. Rowspan
32
+ 8. :span
33
+ 9. Footer
34
+ 10. Header
35
+ 11. Base class for footer, header and tbody.
36
+ 12. Avoid body block if there's no header/footer.
37
+ 13. value: :function cell param
38
+ 14. merge_html_options?
39
+ 15. Bypass :value param.
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task :default => :spec
data/grid_fu.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grid_fu/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "grid_fu"
8
+ gem.version = GridFu::VERSION
9
+ gem.authors = ["Victor Sokolov"]
10
+ gem.email = ["gzigzigzeo@gmail.com"]
11
+ gem.description = %q{HTML table generator}
12
+ gem.summary = %q{HTML table generator}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'active_support', '~> 3'
21
+
22
+ gem.add_development_dependency 'rspec'
23
+ gem.add_development_dependency 'rake'
24
+ end
@@ -0,0 +1,28 @@
1
+ module GridFu
2
+ class Body < Element
3
+ attr_reader :rows
4
+
5
+ def initialize(*args, &block)
6
+ self.rows = []
7
+
8
+ config.tag ||= 'tbody'
9
+
10
+ super
11
+ end
12
+
13
+ protected
14
+ def html_content(collection, resource_class)
15
+ html = collection.map do |member|
16
+ rows.map.with_index { |row, index| row.to_html(member, index) }.join(' ')
17
+ end
18
+ html.join
19
+ end
20
+
21
+ protected
22
+ def row(*args, &block)
23
+ self.rows << Row.new(&block)
24
+ end
25
+
26
+ attr_writer :rows
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ module GridFu
2
+ class Cell < Element
3
+ def initialize(*args, &block)
4
+ self.value = block
5
+ self.key = args.first if args.first.is_a?(String) or args.first.is_a?(Symbol)
6
+
7
+ config.tag = 'td'
8
+ super(*args, &nil) # Bypass block evaling: in this case it's a value formatter
9
+ end
10
+
11
+ attr_reader :key, :value
12
+
13
+ def to_html(member, index, &block)
14
+ member_value = member.send(key) if key.present? and member.respond_to?(key)
15
+ member_value = if value.is_a?(Proc)
16
+ value.call(member_value, member, index)
17
+ else
18
+ member_value.to_s
19
+ end
20
+
21
+ super(*[member_value, member, index], &block)
22
+ end
23
+
24
+ protected
25
+ def html_content(value, member, index)
26
+ value
27
+ end
28
+
29
+ attr_writer :value
30
+ attr_writer :key
31
+ end
32
+ end
@@ -0,0 +1,78 @@
1
+ module GridFu
2
+ class Element
3
+ include ActiveSupport::Configurable
4
+
5
+ def initialize(*args, &block)
6
+ instance_exec(&block) if block_given?
7
+
8
+ config.html_options ||= {}
9
+
10
+ set_options([:tag, :html_options], *args)
11
+ end
12
+
13
+ def to_html(*args, &block)
14
+ tag, html_options = get_options([:tag, :html_options], *args)
15
+
16
+ html_options = _to_html_args(html_options)
17
+
18
+ html = []
19
+ html << "<#{tag}"
20
+ if html_options.present?
21
+ html << " #{html_options}"
22
+ end
23
+ html << '>'
24
+ if block_given?
25
+ html << yield(*args)
26
+ else
27
+ html << html_content(*args).to_s
28
+ end
29
+ html << "</#{tag}>"
30
+
31
+ html.join
32
+ end
33
+
34
+ protected
35
+ def html_content(*args)
36
+ raise NotImplementedError, "Must implement #html_content for #{self.class.name} or pass a block for #to_html"
37
+ end
38
+
39
+ def set_options(expected, *args)
40
+ options = args.extract_options!
41
+
42
+ expected.each do |name|
43
+ config[name] = options.delete(name) if options.key?(name)
44
+ end
45
+ end
46
+
47
+ def get_options(expected, *args)
48
+ expected.map do |name|
49
+ config[name].is_a?(Proc) ? config[name].call(*args) : config[name]
50
+ end
51
+ end
52
+
53
+ def self.option_setter(name)
54
+ define_method name do |value = nil, &block|
55
+ if value.present? || block.present?
56
+ config[name] = value || block
57
+ end
58
+ end
59
+ end
60
+
61
+ option_setter :tag
62
+ option_setter :html_options
63
+
64
+ private
65
+ def _to_html_args(options, prepend = nil)
66
+ options = options || {}
67
+ html_args = options.map do |key, value|
68
+ if value.is_a?(Hash)
69
+ _to_html_args(value, key)
70
+ else
71
+ key = "#{prepend}-#{key}" if prepend.present?
72
+ %{#{key}="#{value}"}
73
+ end
74
+ end
75
+ html_args.join(' ')
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,26 @@
1
+ module GridFu
2
+ class Row < Element
3
+ attr_reader :cells
4
+
5
+ def initialize(*args, &block)
6
+ self.cells = []
7
+ config.tag = 'tr'
8
+
9
+ super
10
+ end
11
+
12
+ protected
13
+ def html_content(member, index)
14
+ html = cells.map do |cell|
15
+ cell.to_html(member, index)
16
+ end
17
+ html.join
18
+ end
19
+
20
+ def cell(*args, &block)
21
+ self.cells << Cell.new(*args, &block)
22
+ end
23
+
24
+ attr_writer :cells
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ module GridFu
2
+ class Table < Element
3
+ attr_reader :header_content, :body_content, :footer_content
4
+
5
+ def initialize(*args, &block)
6
+ config.tag ||= 'table'
7
+
8
+ super
9
+ end
10
+
11
+ protected
12
+ def html_content(collection, resource_class = nil)
13
+ body_content.to_html(collection, resource_class)
14
+ end
15
+
16
+ def body(*args, &block)
17
+ self.body_content = Body.new(*args, &block)
18
+ end
19
+
20
+ attr_writer :header_content, :body_content, :footer_content
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module GridFu
2
+ VERSION = "0.0.1"
3
+ end
data/lib/grid_fu.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'active_support/core_ext/class/attribute_accessors'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'active_support/configurable'
4
+
5
+ require 'grid_fu/version'
6
+ require 'grid_fu/element'
7
+ require 'grid_fu/table'
8
+ require 'grid_fu/row'
9
+ require 'grid_fu/body'
10
+ require 'grid_fu/cell'
11
+
12
+ module GridFu
13
+ def define(*args, &block)
14
+ Table.new(*args, &block)
15
+ end
16
+ module_function :define
17
+ end
data/spec/grid_spec.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Configuration' do
4
+ subject { sample_table }
5
+
6
+ it 'should set provided html options correctly' do
7
+ #subject.config.tag.should == 'table'
8
+ #subject.config.html_options.should == {}
9
+ #subject.body_content.config.tag.should == 'tbody'
10
+ #subject.body_content.rows.first.config.html_options.should == { class: 'odd' }
11
+ #subject.body_content.rows.last.config.html_options.should be_kind_of(Proc)
12
+
13
+ # puts subject.to_html(sample_collection)
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'grid_fu'
5
+ require 'support/sample_table'
6
+
7
+ RSpec.configure do |config|
8
+ # some (optional) config here
9
+ end
@@ -0,0 +1,41 @@
1
+ require 'ostruct'
2
+
3
+ def sample_collection
4
+ [
5
+ OpenStruct.new(id: 1, age: 27, value: 'Jim Morrison'),
6
+ OpenStruct.new(id: 1, age: 70, value: 'William Blake'),
7
+ OpenStruct.new(id: 1, age: 89, value: 'Robert Lee Frost')
8
+ ]
9
+ end
10
+
11
+ def sample_helper_function
12
+ "I hope this helps"
13
+ end
14
+
15
+ def sample_table
16
+ GridFu.define do
17
+ body do
18
+ html_options class: 'sortable'
19
+ row do
20
+ html_options do |member, index|
21
+ { data: { id: member.id } }
22
+ end
23
+
24
+ cell html_options: ->(value, _, _) { { data: { value: value } } } do |_, _, index|
25
+ index
26
+ end
27
+ cell :id
28
+ cell :age do |value, _, _|
29
+ "Dead at #{value}"
30
+ end
31
+ cell do
32
+ sample_helper_function
33
+ end
34
+ end
35
+
36
+ row html_options: { class: 'small' } do
37
+ tag 'div'
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grid_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Victor Sokolov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_support
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: HTML table generator
63
+ email:
64
+ - gzigzigzeo@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - grid_fu.gemspec
76
+ - lib/grid_fu.rb
77
+ - lib/grid_fu/body.rb
78
+ - lib/grid_fu/cell.rb
79
+ - lib/grid_fu/element.rb
80
+ - lib/grid_fu/row.rb
81
+ - lib/grid_fu/table.rb
82
+ - lib/grid_fu/version.rb
83
+ - spec/grid_spec.rb
84
+ - spec/spec_helper.rb
85
+ - spec/support/sample_table.rb
86
+ homepage: ''
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ segments:
99
+ - 0
100
+ hash: 4608333944381045103
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ segments:
108
+ - 0
109
+ hash: 4608333944381045103
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.25
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: HTML table generator
116
+ test_files:
117
+ - spec/grid_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec/support/sample_table.rb