cheveret 2.0.0.rc5 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/cheveret.gemspec +14 -9
- data/lib/cheveret.rb +24 -8
- data/lib/cheveret/{filtering.rb → adapter/active_record.rb} +5 -16
- data/lib/cheveret/adapter/sunspot.rb +57 -0
- data/lib/cheveret/{config.rb → builder/divider.rb} +16 -19
- data/lib/cheveret/column.rb +10 -25
- data/lib/cheveret/helper.rb +47 -8
- data/lib/cheveret/table.rb +47 -0
- data/lib/cheveret/table/columns.rb +77 -0
- data/lib/cheveret/table/data.rb +62 -0
- data/lib/cheveret/{base.rb → table/locale.rb} +24 -27
- data/lib/cheveret/table/mapping.rb +88 -0
- data/lib/cheveret/table/rendering.rb +122 -0
- data/lib/cheveret/table/sizing.rb +136 -0
- data/lib/cheveret/table/sorting.rb +124 -0
- data/lib/cheveret/version.rb +2 -3
- metadata +20 -18
- data/lib/cheveret/dsl.rb +0 -124
- data/lib/cheveret/rendering.rb +0 -85
- data/lib/cheveret/resizing.rb +0 -97
data/lib/cheveret/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cheveret
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 7
|
5
|
+
prerelease: false
|
6
6
|
segments:
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
9
|
- 0
|
10
|
-
|
11
|
-
version: 2.0.0.rc5
|
10
|
+
version: 3.0.0
|
12
11
|
platform: ruby
|
13
12
|
authors:
|
14
13
|
- Ben Caldwell
|
@@ -16,7 +15,7 @@ autorequire:
|
|
16
15
|
bindir: bin
|
17
16
|
cert_chain: []
|
18
17
|
|
19
|
-
date: 2010-
|
18
|
+
date: 2010-12-01 00:00:00 +11:00
|
20
19
|
default_executable:
|
21
20
|
dependencies:
|
22
21
|
- !ruby/object:Gem::Dependency
|
@@ -56,14 +55,19 @@ files:
|
|
56
55
|
- cheveret.gemspec
|
57
56
|
- init.rb
|
58
57
|
- lib/cheveret.rb
|
59
|
-
- lib/cheveret/
|
58
|
+
- lib/cheveret/adapter/active_record.rb
|
59
|
+
- lib/cheveret/adapter/sunspot.rb
|
60
|
+
- lib/cheveret/builder/divider.rb
|
60
61
|
- lib/cheveret/column.rb
|
61
|
-
- lib/cheveret/config.rb
|
62
|
-
- lib/cheveret/dsl.rb
|
63
|
-
- lib/cheveret/filtering.rb
|
64
62
|
- lib/cheveret/helper.rb
|
65
|
-
- lib/cheveret/
|
66
|
-
- lib/cheveret/
|
63
|
+
- lib/cheveret/table.rb
|
64
|
+
- lib/cheveret/table/columns.rb
|
65
|
+
- lib/cheveret/table/data.rb
|
66
|
+
- lib/cheveret/table/locale.rb
|
67
|
+
- lib/cheveret/table/mapping.rb
|
68
|
+
- lib/cheveret/table/rendering.rb
|
69
|
+
- lib/cheveret/table/sizing.rb
|
70
|
+
- lib/cheveret/table/sorting.rb
|
67
71
|
- lib/cheveret/version.rb
|
68
72
|
- rails/init.rb
|
69
73
|
- spec/cheveret_spec.rb
|
@@ -90,14 +94,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
95
|
none: false
|
92
96
|
requirements:
|
93
|
-
- - "
|
97
|
+
- - ">="
|
94
98
|
- !ruby/object:Gem::Version
|
95
|
-
hash:
|
99
|
+
hash: 3
|
96
100
|
segments:
|
97
|
-
-
|
98
|
-
|
99
|
-
- 1
|
100
|
-
version: 1.3.1
|
101
|
+
- 0
|
102
|
+
version: "0"
|
101
103
|
requirements: []
|
102
104
|
|
103
105
|
rubyforge_project:
|
data/lib/cheveret/dsl.rb
DELETED
@@ -1,124 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright (c) 2010 RateCity Pty. Ltd.
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
-
# a copy of this software and associated documentation files (the
|
6
|
-
# "Software"), to deal in the Software without restriction, including
|
7
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
-
# the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be
|
13
|
-
# included in all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
-
#++
|
23
|
-
|
24
|
-
module Cheveret
|
25
|
-
module DSL
|
26
|
-
def header_default(&block)
|
27
|
-
raise NotImplementedError
|
28
|
-
end
|
29
|
-
|
30
|
-
def data_default(&block)
|
31
|
-
raise NotImplementedError
|
32
|
-
end
|
33
|
-
|
34
|
-
# registers a new column with the specified options
|
35
|
-
#
|
36
|
-
# @example minimum required to define a column
|
37
|
-
# column :author
|
38
|
-
#
|
39
|
-
# @example flexible width, sortable column
|
40
|
-
# column :description => [ :flexible, :sortable ]
|
41
|
-
#
|
42
|
-
# @example fixed with column with no header label
|
43
|
-
# column :check_box, :label => false, :width => 30
|
44
|
-
#
|
45
|
-
# @example sortable column with custom header label
|
46
|
-
# column :published => [ :sortable ], :label => "Publish Date"
|
47
|
-
#
|
48
|
-
# @param [Symbol,Array] name_or_hash
|
49
|
-
# the name of the column, optionally combined with any flags that should be
|
50
|
-
# set to +true+
|
51
|
-
#
|
52
|
-
# @param [Hash] options a hash of options for the column
|
53
|
-
#
|
54
|
-
# @option options [Proc] :data
|
55
|
-
#
|
56
|
-
# @option options [Boolean] :flexible (false)
|
57
|
-
# if +true+ the column will resize automatically depending on the size of the
|
58
|
-
# table
|
59
|
-
#
|
60
|
-
# @option options [Proc] :header
|
61
|
-
#
|
62
|
-
# @option options [String,Boolean] :label
|
63
|
-
# used to determine what gets used as a label in the column header. if set to
|
64
|
-
# +false+ no lable will be rendered
|
65
|
-
#
|
66
|
-
# @option options [Boolean] :sortable (false)
|
67
|
-
#
|
68
|
-
# @option options [Integer] :width
|
69
|
-
def column(name_or_hash, options={})
|
70
|
-
if name_or_hash.is_a?(Hash)
|
71
|
-
name = name_or_hash.except(:label, :hint, :width).keys.first
|
72
|
-
name_or_hash.delete(name).each { |k| options[k] = true }
|
73
|
-
options.merge!(name_or_hash)
|
74
|
-
else
|
75
|
-
name = name_or_hash
|
76
|
-
end
|
77
|
-
|
78
|
-
if @columns[name].present?
|
79
|
-
options.each { |k, v| @columns[name].send(:"#{k}=", v) }
|
80
|
-
else
|
81
|
-
@columns[name] = Column.new(name, options)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
# define how to extract and render data value for a particular column
|
86
|
-
#
|
87
|
-
# chevert will call the block you define here to render the inner part of the
|
88
|
-
# table data cell for each object in the collection
|
89
|
-
#
|
90
|
-
# if you don't specify one or more column names when calling this method,
|
91
|
-
# cheveret will assume that you're defining the data block for the last column
|
92
|
-
# you registered
|
93
|
-
#
|
94
|
-
# @example format money values in the view
|
95
|
-
# - column :price
|
96
|
-
# - data do |book|
|
97
|
-
# %span= number_to_currency book.price
|
98
|
-
#
|
99
|
-
# @example define data block for multiple columns
|
100
|
-
# - column :title
|
101
|
-
# - column :author
|
102
|
-
# - data [ :title, :author ] do |column, object|
|
103
|
-
# %p{ :class => column.name }= object.send(column.name)
|
104
|
-
def data(*args, &block)
|
105
|
-
options = args.extract_options!
|
106
|
-
|
107
|
-
[ *args.first || @columns.keys.last ].each do |column_name|
|
108
|
-
column = @columns[column_name ]
|
109
|
-
column.data = block
|
110
|
-
|
111
|
-
instance_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
112
|
-
def data_for_#{column.name}(object)
|
113
|
-
capture(object, &@columns[:#{column.name}].data)
|
114
|
-
end
|
115
|
-
RUBY_EVAL
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def header(*args, &block)
|
120
|
-
raise NotImplementedError
|
121
|
-
end
|
122
|
-
|
123
|
-
end
|
124
|
-
end
|
data/lib/cheveret/rendering.rb
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright (c) 2010 RateCity Pty. Ltd.
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
-
# a copy of this software and associated documentation files (the
|
6
|
-
# "Software"), to deal in the Software without restriction, including
|
7
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
-
# the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be
|
13
|
-
# included in all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
-
#++
|
23
|
-
|
24
|
-
module Cheveret
|
25
|
-
module Rendering
|
26
|
-
def render(collection, options={})
|
27
|
-
options[:class] = [ 'table', options[:class] ].flatten.join(' ').strip
|
28
|
-
|
29
|
-
content_tag(:div, options) do
|
30
|
-
header + body(collection)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def header(options={})
|
35
|
-
row = @columns.values.map do |column|
|
36
|
-
cell(:th, column) do
|
37
|
-
type = column.sortable? ? :a : :span
|
38
|
-
content_tag(type, column.label, {
|
39
|
-
:title => column.hint,
|
40
|
-
})
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
content_tag(:div, :class => 'thead') do
|
45
|
-
content_tag(:div, row, :class => 'tr')
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def body(collection, options={})
|
50
|
-
content_tag(:div, rows(collection), {
|
51
|
-
:class => 'tbody'
|
52
|
-
})
|
53
|
-
end
|
54
|
-
|
55
|
-
def rows(collection, options={})
|
56
|
-
collection.map { |object| row(object) }.join
|
57
|
-
end
|
58
|
-
|
59
|
-
# render a single table row for the specified data object
|
60
|
-
#
|
61
|
-
# @param [Object] object
|
62
|
-
# @param [Hash] options
|
63
|
-
#
|
64
|
-
# @option options [Array] :only
|
65
|
-
# @option options [Array] :except
|
66
|
-
# @option options [Array,String] :class
|
67
|
-
# @option options [Integer] :width
|
68
|
-
def row(object, options={})
|
69
|
-
alt = cycle('', 'alt', :name => 'cheveret')
|
70
|
-
options[:class] = [ 'tr', alt, options[:class]].flatten.join(' ').strip
|
71
|
-
|
72
|
-
content_tag(:div, options) do
|
73
|
-
@columns.values.map do |column|
|
74
|
-
cell(:td, column) { send(:"data_for_#{column.name}", object) rescue nil }
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def cell(type, column, options={}, &block)
|
80
|
-
options[:class] = [ type, column.name, options[:class] ].flatten.join(' ').strip
|
81
|
-
content_tag(:div, yield, options)
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
end
|
data/lib/cheveret/resizing.rb
DELETED
@@ -1,97 +0,0 @@
|
|
1
|
-
#--
|
2
|
-
# Copyright (c) 2010 RateCity Pty. Ltd.
|
3
|
-
#
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
-
# a copy of this software and associated documentation files (the
|
6
|
-
# "Software"), to deal in the Software without restriction, including
|
7
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
-
# the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be
|
13
|
-
# included in all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
-
#++
|
23
|
-
|
24
|
-
module Cheveret
|
25
|
-
module Resizing
|
26
|
-
def width
|
27
|
-
@width || 0
|
28
|
-
end
|
29
|
-
|
30
|
-
# set width constraint for the table.
|
31
|
-
#
|
32
|
-
# if the width value is different to that previously set, cheveret will call the
|
33
|
-
# #resize! method to adjust the widths of flexible columns to fit
|
34
|
-
#
|
35
|
-
# @param [Integer] new_width the maximum width of the table in pixels
|
36
|
-
def width=(new_width)
|
37
|
-
return @width if new_width == @width
|
38
|
-
|
39
|
-
@width = new_width
|
40
|
-
resize!
|
41
|
-
end
|
42
|
-
|
43
|
-
def config(new_config=nil)
|
44
|
-
return super if new_config.nil?
|
45
|
-
|
46
|
-
self.width = new_config.delete(:width) || self.width
|
47
|
-
super
|
48
|
-
end
|
49
|
-
|
50
|
-
# some meta magic - make sure that the table is resized correctly before any of
|
51
|
-
# the render methods start generating output
|
52
|
-
[ :render, :header, :body, :rows ].each do |renderer|
|
53
|
-
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
54
|
-
def #{renderer}(*args)
|
55
|
-
config = args.extract_options!
|
56
|
-
|
57
|
-
width = config.delete(:width)
|
58
|
-
super(*args << config)
|
59
|
-
end
|
60
|
-
RUBY_EVAL
|
61
|
-
end
|
62
|
-
|
63
|
-
def cell(type, column, options={}, &block)
|
64
|
-
options[:style] = "width: #{@widths[column.name] || column.width}px"
|
65
|
-
super
|
66
|
-
end
|
67
|
-
|
68
|
-
# resize flexible columns in attempt to reduce the total width, making sure it
|
69
|
-
# fits within the constraints of the table
|
70
|
-
def resize!
|
71
|
-
@widths = {}
|
72
|
-
|
73
|
-
columns_width = 0
|
74
|
-
flexibles = []
|
75
|
-
|
76
|
-
@columns.values.each do |column|
|
77
|
-
columns_width += column.width
|
78
|
-
flexibles << column if column.flexible?
|
79
|
-
end
|
80
|
-
|
81
|
-
# todo: handle too-many/too-wide columns
|
82
|
-
raise "uh-oh spaghettio-s" if columns_width > @width
|
83
|
-
|
84
|
-
# todo: fix rounding in with calculation
|
85
|
-
# todo: trim last column that fits into table width if necessary
|
86
|
-
if columns_width < @width && !flexibles.empty?
|
87
|
-
padding = (@width - columns_width) / flexibles.length
|
88
|
-
flexibles.each { |column| @widths[column.name] = column.width + padding }
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def weigh!
|
93
|
-
raise NotImplementedError
|
94
|
-
end
|
95
|
-
|
96
|
-
end
|
97
|
-
end
|