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.
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cheveret}
8
- s.version = "2.0.0.rc5"
8
+ s.version = "3.0.0"
9
9
 
10
- s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Caldwell"]
12
- s.date = %q{2010-10-07}
12
+ s.date = %q{2010-12-01}
13
13
  s.description = %q{Generating HTML tables of data in the views of your Rails
14
14
  application is not very DRY even for the simpler of cases. Cheveret allows you to more
15
15
  clearly separate logic and templating and reduce the amount of code in your views.}
@@ -27,14 +27,19 @@ clearly separate logic and templating and reduce the amount of code in your view
27
27
  "cheveret.gemspec",
28
28
  "init.rb",
29
29
  "lib/cheveret.rb",
30
- "lib/cheveret/base.rb",
30
+ "lib/cheveret/adapter/active_record.rb",
31
+ "lib/cheveret/adapter/sunspot.rb",
32
+ "lib/cheveret/builder/divider.rb",
31
33
  "lib/cheveret/column.rb",
32
- "lib/cheveret/config.rb",
33
- "lib/cheveret/dsl.rb",
34
- "lib/cheveret/filtering.rb",
35
34
  "lib/cheveret/helper.rb",
36
- "lib/cheveret/rendering.rb",
37
- "lib/cheveret/resizing.rb",
35
+ "lib/cheveret/table.rb",
36
+ "lib/cheveret/table/columns.rb",
37
+ "lib/cheveret/table/data.rb",
38
+ "lib/cheveret/table/locale.rb",
39
+ "lib/cheveret/table/mapping.rb",
40
+ "lib/cheveret/table/rendering.rb",
41
+ "lib/cheveret/table/sizing.rb",
42
+ "lib/cheveret/table/sorting.rb",
38
43
  "lib/cheveret/version.rb",
39
44
  "rails/init.rb",
40
45
  "spec/cheveret_spec.rb",
@@ -22,13 +22,29 @@
22
22
  #++
23
23
 
24
24
  module Cheveret
25
- autoload :Base, 'cheveret/base'
26
- autoload :Column, 'cheveret/column'
27
- autoload :Config, 'cheveret/config'
28
- autoload :DSL, 'cheveret/dsl'
29
- autoload :Filtering, 'cheveret/filtering'
30
- autoload :Helper, 'cheveret/helper'
31
- autoload :Rendering, 'cheveret/rendering'
32
- autoload :Resizing, 'cheveret/resizing'
25
+ autoload :Column, 'cheveret/column'
26
+ autoload :Helper, 'cheveret/helper'
27
+
28
+ module Adapter
29
+ autoload :ActiveRecord, 'cheveret/adapter/active_record'
30
+ autoload :Sunspot, 'cheveret/adapter/sunspot'
31
+ end
32
+
33
+ module Builder
34
+ autoload :Divider, 'cheveret/builder/divider'
35
+ # autoload :DefinitionList, 'cheveret/builder/definition_list'
36
+ # autoload :Table, 'cheveret/builder/table'
37
+ end
38
+
39
+ module Table
40
+ autoload :Base, 'cheveret/table'
41
+ autoload :Columns, 'cheveret/table/columns'
42
+ autoload :Data, 'cheveret/table/data'
43
+ autoload :Locale, 'cheveret/table/locale'
44
+ autoload :Mapping, 'cheveret/table/mapping'
45
+ autoload :Rendering, 'cheveret/table/rendering'
46
+ autoload :Sizing, 'cheveret/table/sizing'
47
+ autoload :Sorting, 'cheveret/table/sorting'
48
+ end
33
49
 
34
50
  end
@@ -22,20 +22,9 @@
22
22
  #++
23
23
 
24
24
  module Cheveret
25
- module Filtering
26
- def config(new_config=nil)
27
- return super if new_config.nil?
25
+ module Adapter
26
+ module ActiveRecord
28
27
 
29
- except, only = new_config.delete(:except), new_config.delete(:only)
30
- if (except || only)
31
- @columns.reject! { |k, v| !only.include?(k) } if only
32
- # todo: support use of except
33
- end
34
-
35
- super
36
- end
37
-
38
- # todo: wrap around header, body, rows and allow localised :only and :except
39
-
40
- end
41
- end
28
+ end # ActiveRecord
29
+ end # Adapter
30
+ end # Cheveret
@@ -0,0 +1,57 @@
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 Adapter
26
+ module Sunspot
27
+
28
+ ##
29
+ #
30
+ #
31
+ def table_data_for(column, item)
32
+ item.stored(column.name)
33
+ end
34
+
35
+ ##
36
+ #
37
+ #
38
+ [ :current_page,
39
+ :next_page,
40
+ :offset,
41
+ :per_page,
42
+ :total,
43
+ :total_entries,
44
+ :total_pages
45
+ ].each do |proxy|
46
+ # this is really territory of will_paginate
47
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
48
+ def #{proxy}(*args)
49
+ return 0 unless collection.respond_to?(:#{proxy})
50
+ collection.#{proxy}(*args)
51
+ end
52
+ RUBY_EVAL
53
+ end
54
+
55
+ end # Sunspot
56
+ end # Adapter
57
+ end # Cheveret
@@ -22,24 +22,21 @@
22
22
  #++
23
23
 
24
24
  module Cheveret
25
- module Config
26
- # dsl method for defining default configuration options for table rendering
27
- #
28
- # @param [Hash] new_config
29
- # a hash of configuration options. gets merged with any existing config
30
- def config(new_config=nil)
31
- return @config ||= {} if new_config.nil?
32
- config.merge!(new_config)
33
- end
25
+ module Builder
26
+ module Divider
34
27
 
35
- [ :render, :header, :body, :rows ].each do |renderer|
36
- class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
37
- def #{renderer}(*args)
38
- options = config(args.extract_options!)
39
- super(*args << options)
40
- end
41
- RUBY_EVAL
42
- end
28
+ ##
29
+ #
30
+ #
31
+ [ :table, :thead, :tbody, :tr, :th, :td ].each do |tag|
32
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
33
+ def #{tag}_tag(options={})
34
+ options[:class] = [ '#{tag}', options[:class] ].flatten.join(' ').strip
35
+ template.content_tag(:div, options) { yield }
36
+ end
37
+ RUBY_EVAL
38
+ end
43
39
 
44
- end
45
- end
40
+ end # Divider
41
+ end # Builder
42
+ end # Cheveret
@@ -23,34 +23,19 @@
23
23
 
24
24
  module Cheveret
25
25
  class Column
26
- attr_accessor :name, :data, :label, :hint, :weight, :width
27
- attr_accessor :flexible, :sortable
26
+ attr_accessor :name, :weight
27
+ attr_accessor :th_html, :td_html
28
28
 
29
- def initialize(name, config={})
30
- config.merge(:name => name).each do |k, v|
31
- instance_variable_set(:"@#{k}", v) if respond_to?(:"#{k}=")
32
- end
29
+ def initialize(name, options=nil)
30
+ @name = name
31
+ config(options) unless options.nil?
33
32
  end
34
33
 
35
- def flexible?
36
- @flexible == true
37
- end
38
-
39
- def label
40
- case @label
41
- when nil then @name.to_s.humanize # todo: support i18n for column labels
42
- when false then nil
43
- else @label
44
- end
45
- end
46
-
47
- # returns +true+ unless a column has explicitly set <tt>:sortable => false</tt>
48
- def sortable?
49
- @sortable == true
50
- end
51
-
52
- def width
53
- @width || 0
34
+ ##
35
+ #
36
+ #
37
+ def config(options)
38
+ options.each { |k, v| send(:"#{k}=", v) if respond_to?(:"#{k}=") }
54
39
  end
55
40
 
56
41
  end
@@ -23,15 +23,54 @@
23
23
 
24
24
  module Cheveret
25
25
  module Helper
26
- def define_table(options={}, &block)
27
- builder = options.delete(:builder) || ActionView::Base.default_table_builder
28
- builder.new(self, &block)
26
+
27
+ ##
28
+ #
29
+ #
30
+ def cheveret_table(table, options={})
31
+ table.template = self
32
+ options.keys.each do |option|
33
+ table.send(:"#{option}=", options.delete(option)) if table.respond_to?(:"#{option}=")
34
+ end
35
+
36
+ table.render_table(options)
37
+ end
38
+
39
+ ##
40
+ #
41
+ #
42
+ def cheveret_head(table, options)
43
+ table.template = self
44
+ options.keys.each do |option|
45
+ table.send(:"#{option}=", options.delete(option)) if table.respond_to?(:"#{option}=")
46
+ end
47
+
48
+ table.render_thead(options)
29
49
  end
30
50
 
31
- ActionView::Base.class_eval do
32
- cattr_accessor :default_table_builder
33
- self.default_table_builder = Cheveret::Base
51
+ ##
52
+ #
53
+ #
54
+ def cheveret_body(table, options={})
55
+ table.template = self
56
+ options.keys.each do |option|
57
+ table.send(:"#{option}=", options.delete(option)) if table.respond_to?(:"#{option}=")
58
+ end
59
+
60
+ table.render_tbody(options)
61
+ end
62
+
63
+ ##
64
+ #
65
+ #
66
+ def cheveret_rows(table, options={})
67
+ table.template = self
68
+ options.keys.each do |option|
69
+ table.send(:"#{option}=", options.delete(option)) if table.respond_to?(:"#{option}=")
70
+ end
71
+
72
+ table.render_rows(options)
34
73
  end
35
74
 
36
- end
37
- end
75
+ end # Helper
76
+ end # Cheveret
@@ -0,0 +1,47 @@
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 Table
26
+ class Base
27
+ include Cheveret::Table::Columns
28
+ include Cheveret::Table::Data
29
+ include Cheveret::Table::Mapping
30
+ include Cheveret::Table::Rendering
31
+ include Cheveret::Table::Locale
32
+ include Cheveret::Table::Sizing
33
+ include Cheveret::Table::Sorting
34
+
35
+ include Cheveret::Builder::Divider
36
+ #include Cheveret::Adapter::ActiveRecord
37
+
38
+ ##
39
+ #
40
+ #
41
+ def config(options)
42
+ options.each { |k, v| send(:"#{k}=", v) if respond_to?(:"#{k}=") }
43
+ end
44
+
45
+ end # Base
46
+ end # Table
47
+ end # Cheveret
@@ -0,0 +1,77 @@
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 Table
26
+ module Columns
27
+
28
+ def self.included(base)
29
+ base.module_eval do
30
+ extend ClassMethods
31
+ end
32
+ end
33
+
34
+ module ClassMethods
35
+
36
+ ##
37
+ # returns an ordered hash of all defined table columns
38
+ #
39
+ def columns
40
+ @columns ||= ::ActiveSupport::OrderedHash.new
41
+ end
42
+
43
+ ##
44
+ # define a new (or re-define an existing) table column
45
+ #
46
+ def column(*args)
47
+ options = args.extract_options!
48
+ raise ArgumentError if args.empty?
49
+
50
+ column_name = args.first
51
+
52
+ # only create new column if one doesn't already exist with specified name
53
+ columns[column_name] ||= Column.new(column_name)
54
+ columns[column_name].tap do |column|
55
+ column.config(options)
56
+ end
57
+ end
58
+
59
+ ##
60
+ #
61
+ #
62
+ def remove_column(column_name)
63
+ columns.delete(column_name)
64
+ end
65
+
66
+ end # ClassMethods
67
+
68
+ ##
69
+ # instance proxy method to get columns defined by the class
70
+ #
71
+ def columns
72
+ @column ||= self.class.columns.dup
73
+ end
74
+
75
+ end # Columns
76
+ end # Table
77
+ end # Cheveret
@@ -0,0 +1,62 @@
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 Table
26
+ module Data
27
+
28
+ attr_accessor :collection
29
+
30
+ ##
31
+ #
32
+ #
33
+ def initialize(collection=[], options={})
34
+ if block_given?
35
+ yield self
36
+ else
37
+ config(options.merge({ :collection => collection }))
38
+ end
39
+ end
40
+
41
+ ##
42
+ #
43
+ #
44
+ def collection
45
+ @collection ||= []
46
+ end
47
+
48
+ ##
49
+ #
50
+ #
51
+ [ :each, :empty?, :length, :map ].each do |proxy|
52
+ # there is a nicer way to do this using forwardable, but this works for now
53
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
54
+ def #{proxy}(*args)
55
+ collection.#{proxy}(*args)
56
+ end
57
+ RUBY_EVAL
58
+ end
59
+
60
+ end # Data
61
+ end # Table
62
+ end #Cheveret