tabulatr2 0.8.9 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,8 +22,8 @@
22
22
 
23
23
  form.form.form-horizontal.tabulatr_filter_form data-table=table_id data-remote="true" role='form'
24
24
  input name="#{formatted_name}_sort" type="hidden" value=table_options[:order_by]
25
- - if table_options[:filter] && columns.filtered_columns.present?
26
- - columns.filtered_columns.each do |column|
25
+ - if table_options[:filter] && columns.select(&:filter).present?
26
+ - columns.select(&:filter).each do |column|
27
27
  - name = column.full_name
28
28
  .control-group.form-group data-filter-column-name=name
29
29
  - iname = "#{formatted_name}_filter[#{name}]"
@@ -19,12 +19,12 @@
19
19
  / OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  / WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
- - if table_options[:filter] && columns.filtered_columns.present?
22
+ - if table_options[:filter] && columns.select(&:filter).present?
23
23
  .dropdown
24
24
  a.btn.btn-default data-toggle="dropdown" href="#"
25
25
  i.icon-filter.glyphicon.glyphicon-filter.fa.fa-filter'
26
26
  i.icon-caret-down.glyphicon.glyphicon-caret-down.fa.fa-caret-down
27
27
  ul.dropdown-menu role="menu" aria-labelledby="dLabel"
28
- - columns.filtered_columns.each do |column|
28
+ - columns.select(&:filter).each do |column|
29
29
  li
30
30
  = link_to column.human_name, '#', "data-show-table-filter" => column.full_name
@@ -19,6 +19,6 @@
19
19
  / OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  / WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
- form#product_fuzzy_search.form-inline.tabulatr-fuzzy-search.pull-right data-table=table_id role="form"
22
+ form.form-inline.tabulatr-fuzzy-search.pull-right data-table=table_id role="form"
23
23
  .form-group
24
24
  input.form-control.search id="#{formatted_name}_fuzzy_search_query" placeholder="Search" type="search"
@@ -19,7 +19,7 @@
19
19
  / OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  / WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
- table.table.tabulatr_table
22
+ table.table.tabulatr_table.tabulatr_static_table
23
23
  thead
24
24
  tr
25
25
  - columns.each do |column|
@@ -27,7 +27,7 @@ table.table.tabulatr_table
27
27
  = column.human_name
28
28
  tbody
29
29
  - records.each do |record|
30
- tr
30
+ tr data-id=record.try(:id)
31
31
  - columns.each do |column|
32
32
  td data-tabulatr-column-name=column.full_name data-tabulatr-type=column.coltype style=column.cell_style
33
33
  = column.value_for(record, self)
@@ -33,7 +33,7 @@ class Tabulatr::Data
33
33
  @includes = Set.new()
34
34
  @cname = @base.name.downcase
35
35
  @batch_actions = nil
36
- table_columns.klass = @base
36
+ @row = self.class.instance_variable_get('@row')
37
37
  table_columns.map do |col|
38
38
  col.klass = @base
39
39
  end
@@ -139,4 +139,5 @@ require_relative './invoker'
139
139
  require_relative './sorting'
140
140
  require_relative './pagination'
141
141
  require_relative './formatting'
142
+ require_relative './row'
142
143
  require_relative './proxy'
@@ -26,7 +26,7 @@ module Tabulatr::Data::DSL
26
26
  def column(name, sort_sql: nil, filter_sql: nil, sql: nil, table_column_options: {}, &block)
27
27
  @columns ||= HashWithIndifferentAccess.new
28
28
  table_column = Tabulatr::Renderer::Column.from(table_column_options.merge(name: name, klass: @base))
29
- @table_columns ||= Tabulatr::Renderer::Columns.new(nil)
29
+ @table_columns ||= []
30
30
  @table_columns << table_column
31
31
 
32
32
  @columns[name.to_sym] = {
@@ -41,7 +41,7 @@ module Tabulatr::Data::DSL
41
41
  def association(assoc, name, sort_sql: nil, filter_sql: nil, sql: nil, table_column_options: {}, &block)
42
42
  @assocs ||= HashWithIndifferentAccess.new
43
43
  @assocs[assoc.to_sym] ||= {}
44
- @table_columns ||= Tabulatr::Renderer::Columns.new(nil)
44
+ @table_columns ||= []
45
45
  table_column = Tabulatr::Renderer::Association.from(table_column_options.merge(name: name, table_name: assoc, klass: @base))
46
46
  @table_columns << table_column
47
47
 
@@ -59,6 +59,17 @@ module Tabulatr::Data::DSL
59
59
  @search = args.presence || block
60
60
  end
61
61
 
62
+ def checkbox(table_column_options: {})
63
+ @table_columns ||= []
64
+ box = Tabulatr::Renderer::Checkbox.from(table_column_options.merge(klass: @base, filter: false, sortable: false))
65
+ @table_columns << box
66
+ end
67
+
68
+ def row &block
69
+ raise 'Please pass a block to row if you want to use it.' unless block_given?
70
+ @row = block
71
+ end
72
+
62
73
  end
63
74
 
64
75
  Tabulatr::Data.send :extend, Tabulatr::Data::DSL
@@ -62,24 +62,17 @@ module Tabulatr::Data::Filtering
62
62
  end
63
63
 
64
64
  def apply_condition(n,v)
65
- like ||= Tabulatr::Utility.like_statement
66
65
  if v.is_a?(String)
67
- @relation = @relation.where("#{n} = ?", v) unless v.blank?
66
+ apply_string_condition("#{n} = ?", v)
68
67
  elsif v.is_a?(Hash)
69
- if v[:like].present?
70
- @relation = @relation.where("#{n} #{like} ?", "%#{v[:like]}%")
71
- elsif v[:date].present?
72
- apply_date_condition(n, v[:date])
73
- else
74
- @relation = @relation.where("#{n} >= ?", "#{v[:from]}") if v[:from].present?
75
- @relation = @relation.where("#{n} <= ?", "#{v[:to]}") if v[:to].present?
76
- end
68
+ apply_hash_condition(n, v)
77
69
  else
78
70
  raise "Wrong filter type: #{v.class}"
79
71
  end
80
72
  end
81
73
 
82
74
  def apply_date_condition(n, cond)
75
+ return unless cond.present?
83
76
  today = Date.today
84
77
  case cond[:simple]
85
78
  when 'none' then return
@@ -88,7 +81,7 @@ module Tabulatr::Data::Filtering
88
81
  when 'this_week' then since = today.at_beginning_of_week
89
82
  when 'last_7_days' then since = today - 7.day
90
83
  when 'this_month' then since = today.at_beginning_of_month
91
- when 'last_30_days' then since = today. - 30.day
84
+ when 'last_30_days' then since = today - 30.day
92
85
  when 'from_to'
93
86
  since = Date.parse(cond[:from]) if cond[:from].present?
94
87
  @relation = @relation.where("#{n} <= ?", Date.parse(cond[:to])) if cond[:to].present?
@@ -96,6 +89,18 @@ module Tabulatr::Data::Filtering
96
89
  @relation = @relation.where("#{n} >= ?", since) if since.present?
97
90
  end
98
91
 
92
+ def apply_string_condition(replacement_string, value)
93
+ @relation = @relation.where(replacement_string, value) if value.present?
94
+ end
95
+
96
+ def apply_hash_condition(column_name, hash)
97
+ like ||= Tabulatr::Utility.like_statement
98
+ apply_string_condition("#{column_name} #{like} ?", "%#{hash[:like]}%") if hash[:like].present?
99
+ apply_date_condition(column_name, hash[:date])
100
+ apply_string_condition("#{column_name} >= ?", "#{hash[:from]}")
101
+ apply_string_condition("#{column_name} <= ?", "#{hash[:to]}")
102
+ end
103
+
99
104
  end
100
105
 
101
106
  Tabulatr::Data.send :include, Tabulatr::Data::Filtering
@@ -37,6 +37,7 @@ module Tabulatr::Data::Formatting
37
37
  h[table_name][name] = format_association(record, table_name, name, opts, view)
38
38
  end
39
39
  end # @assocs each
40
+ h[:_row_config] = format_row(view, @row)
40
41
  h
41
42
  end # @relation map
42
43
  end # apply_formats
@@ -54,7 +55,14 @@ module Tabulatr::Data::Formatting
54
55
  opts[:table_column].value_for(record, view)
55
56
  end
56
57
 
57
-
58
+ def format_row(view, row)
59
+ row_config = Row.new
60
+ view.instance_exec(view.record, row_config.attributes, &row) if row.is_a?(Proc)
61
+ view.record.define_singleton_method(:_row_config) do
62
+ row_config.attributes
63
+ end
64
+ row_config.attributes
65
+ end
58
66
  end
59
67
 
60
68
  Tabulatr::Data.send :include, Tabulatr::Data::Formatting
@@ -0,0 +1,8 @@
1
+ class Tabulatr::Data::Formatting::Row
2
+
3
+ attr_accessor :attributes
4
+
5
+ def initialize
6
+ self.attributes = {class: 'tabulatr-row'}
7
+ end
8
+ end
@@ -36,7 +36,7 @@ module Tabulatr::Data::Sorting
36
36
  table_name = @base.reflect_on_association(klass.to_sym).try(:table_name)
37
37
  end
38
38
  nn = build_column_name(col_name, table_name: table_name, assoc_name: assoc_name, use_for: :sort)
39
- raise "asasa" unless ['asc', 'desc'].member?(orientation.downcase)
39
+ raise "Invalid sorting orientation" unless ['asc', 'desc'].member?(orientation.downcase)
40
40
  @relation = @relation.order("#{nn} #{orientation}")
41
41
  else
42
42
  @relation = @relation.order(default_order || "#{@table_name}.#{@base.primary_key} desc")
@@ -1,18 +1,12 @@
1
- <% module_namespacing do %>
2
- class <%= class_name %>TabulatrData < Tabulatr::Data
3
- <% attributes = attributes_names %>
4
- <% if class_name.constantize.table_exists? %>
5
- <% attributes << class_name.constantize.column_names.map(&:to_sym) %>
6
- <% end %>
7
- <% attributes = attributes.flatten.uniq %>
8
- <% if attributes.any? %>
9
- search :<%= attributes.last %>
10
- <% end %>
11
- <% attributes.each do |a| %>
12
- column :<%= a %>
13
- <% end %>
14
- <% association_names.each do |attribute| %>
15
- association :<%= attribute %>, :id
16
- <% end %>
17
- end
1
+ <% module_namespacing do %>class <%= class_name %>TabulatrData < Tabulatr::Data
2
+ <% attributes = attributes_names %><% if class_name.constantize.table_exists? %><% attributes << class_name.constantize.column_names.map(&:to_sym) %><% end %><% attributes = attributes.flatten.uniq %><% if attributes.any? %>
3
+ search :<%= attributes.last %>
4
+ <% end %>
5
+ <% attributes.each do |a| %>
6
+ column :<%= a %>
18
7
  <% end %>
8
+ <% association_names.each do |attribute| %>
9
+ association :<%= attribute %>, :id
10
+ <% end %>
11
+ end
12
+ <% end %>
@@ -37,6 +37,7 @@ module Tabulatr::JsonBuilder
37
37
  attrs.each do |at|
38
38
  insert_attribute_in_hash(at, f, tmp)
39
39
  end
40
+ tmp['_row_config'] = f['_row_config']
40
41
  result << tmp
41
42
  end
42
43
  { data: result, meta: data.__pagination }
@@ -75,18 +76,19 @@ module Tabulatr::JsonBuilder
75
76
  # r["#{at[:relation]}:#{at[:action]}"] = f.try(at[:relation]).try(at[:action])
76
77
  # end
77
78
  begin
79
+ raise Tabulatr::RequestDataNotIncludedError.raise_error(rel, f) if !f.has_key?(rel)
80
+ raise Tabulatr::RequestDataNotIncludedError.raise_error(action, rel) if !f[rel].has_key?(action)
78
81
  r["#{at[:relation]}:#{at[:action]}"] = f[rel][action]
79
- rescue NoMethodError => e
80
- raise $!, "You requested '#{at[:action]}' on '#{at[:relation]}' but
81
- there was no such method included in your TabulatrData", $!.backtrace
82
+ rescue TypeError, NoMethodError => e
83
+ Tabulatr::RequestDataNotIncludedError.raise_error(at[:action], at[:relation])
82
84
  end
83
85
  else
84
86
  begin
85
87
  action = at[:action].to_sym
88
+ raise Tabulatr::RequestDataNotIncludedError.raise_error(action, f) if !f.has_key?(action) && action != :checkbox
86
89
  r[at[:action]] = f[action]
87
- rescue NoMethodError => e
88
- raise $!, "You requested '#{at[:action]}' but
89
- there was no such method included in your TabulatrData", $!.backtrace
90
+ rescue TypeError, NoMethodError => e
91
+ raise Tabulatr::RequestDataNotIncludedError.raise_error(action, f)
90
92
  end
91
93
  end
92
94
  r
@@ -23,8 +23,8 @@
23
23
 
24
24
  class ActionView::Base
25
25
  # render the table in a view
26
- def table_for(klass, opts={}, &block)
27
- Tabulatr::Renderer.build_table(klass, self, opts, &block)
26
+ def table_for(klass, columns: [], **opts, &block)
27
+ Tabulatr::Renderer.build_table(klass, self, opts, columns, &block)
28
28
  end
29
29
 
30
30
  def static_table_for(records, opts={}, &block)
@@ -34,7 +34,7 @@ class Tabulatr::Renderer::Association < Tabulatr::Renderer::Column
34
34
 
35
35
  def principal_value(record)
36
36
  v = record.send(table_name)
37
- if v && v.respond_to?(:to_a) && map
37
+ if v && v.respond_to?(:to_a) && map && name != :count
38
38
  v.map(&:"#{name}")
39
39
  else
40
40
  v.try(name)
@@ -29,7 +29,7 @@ class Tabulatr::Renderer::Checkbox < Tabulatr::Renderer::Column
29
29
  def coltype() 'checkbox' end
30
30
  def column?() false end
31
31
  def checkbox?() true end
32
-
32
+ def full_name() 'checkbox' end
33
33
  def value_for(record, view)
34
34
  nil
35
35
  end
@@ -27,7 +27,7 @@ class Tabulatr::Renderer
27
27
 
28
28
  def initialize(klass)
29
29
  @klass = klass
30
- @columns ||= Columns.new(klass)
30
+ @columns ||= []
31
31
  end
32
32
 
33
33
  def column(name, opts={}, &block)
@@ -49,10 +49,12 @@ class Tabulatr::Renderer
49
49
  @classname = @klass.name.underscore
50
50
  end
51
51
 
52
- def build_table(&block)
52
+ def build_table(columns, &block)
53
53
  tdc = "#{@klass.name}TabulatrData".constantize.new(@klass)
54
54
  if block_given?
55
55
  @columns = ColumnsFromBlock.process @klass, &block
56
+ elsif columns.any?
57
+ @columns = get_requested_columns(tdc.table_columns, columns)
56
58
  else
57
59
  @columns = tdc.table_columns
58
60
  end
@@ -84,8 +86,17 @@ class Tabulatr::Renderer
84
86
  new(klass, view, toptions).build_static_table(records, &block)
85
87
  end
86
88
 
87
- def self.build_table(klass, view, toptions={}, &block)
88
- new(klass, view, toptions).build_table(&block)
89
+ def self.build_table(klass, view, toptions={}, columns, &block)
90
+ new(klass, view, toptions).build_table(columns, &block)
91
+ end
92
+
93
+ private
94
+
95
+ def get_requested_columns(available_columns, requested_columns)
96
+ requested_columns.collect do |r|
97
+ r = "#{r.keys.first}:#{r.values.first}" if r.is_a?(Hash) && r.count == 1
98
+ available_columns.select{|column| column.full_name.to_sym == r.to_sym }
99
+ end.flatten
89
100
  end
90
101
 
91
102
  end
@@ -94,6 +105,5 @@ require_relative './column'
94
105
  require_relative './association'
95
106
  require_relative './action'
96
107
  require_relative './checkbox'
97
- require_relative './columns'
98
108
  require_relative './columns_from_block'
99
109
 
@@ -0,0 +1,9 @@
1
+ module Tabulatr
2
+ class RequestDataNotIncludedError < StandardError
3
+
4
+ def self.raise_error(method, object)
5
+ raise self, "You requested '#{method}' on '#{object}' but
6
+ there was no such method included in your TabulatrData"
7
+ end
8
+ end
9
+ end
@@ -22,5 +22,5 @@
22
22
  #++
23
23
 
24
24
  module Tabulatr
25
- VERSION = "0.8.9"
25
+ VERSION = "0.9.0"
26
26
  end
@@ -8,7 +8,7 @@ class ProductTabulatrData < Tabulatr::Data
8
8
 
9
9
  column :title
10
10
  column :id
11
- column :price do "#{record.price} EUR" end # <- Block evaluiert im Kontext EINES Records
11
+ column :price, table_column_options: {filter: :range} do "#{record.price} EUR" end # <- Block evaluiert im Kontext EINES Records
12
12
  column :edit_link do link_to "edit #{record.title}", product_path(record) end
13
13
  # column :name,
14
14
  # sort: "firstname || ' ' || lastname"
@@ -16,14 +16,18 @@ class ProductTabulatrData < Tabulatr::Data
16
16
  # do
17
17
  # "#{firstname} #{lastname}"
18
18
  # end
19
- column :vendor_product_name, sort_sql: "products.title || '' || vendors.name", filter_sql: "products.title || '' || vendors.name" do
19
+ column :vendor_product_name,
20
+ sort_sql: "products.title || '' || vendors.name",
21
+ filter_sql: "products.title || '' || vendors.name",
22
+ table_column_options: {header: 'Product by vendor'} do
20
23
  "#{record.title} from #{record.vendor.try(:name)}"
21
24
  end
22
- column :active
25
+ column :active, table_column_options: {sortable: false}
23
26
  column :updated_at, table_column_options: { filter: :date } do "#{record.updated_at.strftime('%H:%M %d.%m.%Y')}" end
24
- association :vendor, :name
27
+ association :vendor, :name, table_column_options: { filter: :exact }
25
28
  association :tags, :title do |r|
26
29
  "'#{r.tags.map(&:title).map(&:upcase).join(', ')}'"
27
30
  end
31
+ association :tags, :count
28
32
 
29
33
  end
@@ -1,11 +1,4 @@
1
1
  <%= table_for Product, :paginate => true,
2
- batch_actions: {a: "Hallo", b: "Du da!"} do |t|
3
- t.checkbox
4
- t.column :title, sortable: true
5
- t.column :price, filter: :range
6
- t.column :active, sortable: false
7
- t.column :vendor_product_name, header: 'Product by vendor'
8
- t.column :updated_at, filter: :date
9
- t.association :vendor, :name, filter: :exact
10
- t.association :tags, :title
11
- end %>
2
+ batch_actions: {a: "Hallo", b: "Du da!"},
3
+ columns: [:title, :price, :active, :vendor_product_name, :updated_at,
4
+ {vendor: :name}, 'tags:title'] %>
@@ -6,5 +6,5 @@
6
6
  t.column :vendor_product_name, header: 'Product by vendor'
7
7
  t.column :updated_at, filter: :date
8
8
  t.association :vendor, :name, filter: :exact
9
- t.association :tags, :titletd
9
+ t.association :tags, :title
10
10
  end %>
@@ -14,7 +14,7 @@ module Dummy
14
14
  # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15
15
  # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16
16
  # config.time_zone = 'Central Time (US & Canada)'
17
-
17
+ config.i18n.enforce_available_locales = true
18
18
  # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
19
  # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
20
  # config.i18n.default_locale = :de
@@ -87,7 +87,7 @@ describe "Tabulatr" do
87
87
  end
88
88
  product.save
89
89
  visit count_tags_products_path
90
- page.should have_content 3
90
+ page.find('tbody td[data-tabulatr-column-name="tags:count"]').should have_content 3
91
91
  end
92
92
  end
93
93
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabulatr2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.9
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Horn
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-12-02 00:00:00.000000000 Z
13
+ date: 2014-01-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -95,6 +95,7 @@ files:
95
95
  - lib/tabulatr/data/invoker.rb
96
96
  - lib/tabulatr/data/pagination.rb
97
97
  - lib/tabulatr/data/proxy.rb
98
+ - lib/tabulatr/data/row.rb
98
99
  - lib/tabulatr/data/sorting.rb
99
100
  - lib/tabulatr/engine.rb
100
101
  - lib/tabulatr/generators/railtie.rb
@@ -111,9 +112,9 @@ files:
111
112
  - lib/tabulatr/renderer/association.rb
112
113
  - lib/tabulatr/renderer/checkbox.rb
113
114
  - lib/tabulatr/renderer/column.rb
114
- - lib/tabulatr/renderer/columns.rb
115
115
  - lib/tabulatr/renderer/columns_from_block.rb
116
116
  - lib/tabulatr/renderer/renderer.rb
117
+ - lib/tabulatr/utility/request_data_not_included_error.rb
117
118
  - lib/tabulatr/utility/utility.rb
118
119
  - lib/tabulatr/version.rb
119
120
  - lib/tabulatr2.rb
@@ -200,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
200
201
  version: '0'
201
202
  requirements: []
202
203
  rubyforge_project:
203
- rubygems_version: 2.0.6
204
+ rubygems_version: 2.0.3
204
205
  signing_key:
205
206
  specification_version: 4
206
207
  summary: A tight DSL to build tables of ActiveRecord models with sorting, pagination,
@@ -267,3 +268,4 @@ test_files:
267
268
  - spec/features/tabulatrs_spec.rb
268
269
  - spec/lib/tabulatr/data/data_spec.rb
269
270
  - spec/spec_helper.rb
271
+ has_rdoc: