tabulatr2 0.6.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/README.md +71 -27
  4. data/app/assets/javascripts/tabulatr/application.js +130 -133
  5. data/app/assets/stylesheets/tabulatr/application.css.scss +153 -0
  6. data/app/assets/stylesheets/tabulatr/bootstrap2_fixes.css.scss +24 -0
  7. data/app/views/tabulatr/_tabulatr_actual_table.html.slim +35 -0
  8. data/app/views/tabulatr/_tabulatr_batch_actions_menu.html.slim +34 -0
  9. data/app/views/tabulatr/_tabulatr_filter_dialog.html.slim +84 -0
  10. data/app/views/tabulatr/_tabulatr_filter_menu.html.slim +30 -0
  11. data/app/views/tabulatr/_tabulatr_fuzzy_search_field.html.slim +24 -0
  12. data/app/views/tabulatr/_tabulatr_info_string.html.slim +23 -0
  13. data/app/views/tabulatr/_tabulatr_paginator.html.slim +24 -0
  14. data/app/views/tabulatr/_tabulatr_static_table.html.slim +33 -0
  15. data/app/views/tabulatr/_tabulatr_table.html.slim +45 -0
  16. data/lib/tabulatr/data/column_name_builder.rb +86 -0
  17. data/lib/tabulatr/data/data.rb +135 -0
  18. data/lib/tabulatr/data/dsl.rb +61 -0
  19. data/lib/tabulatr/data/filtering.rb +101 -0
  20. data/lib/tabulatr/data/formatting.rb +65 -0
  21. data/lib/tabulatr/data/invoker.rb +37 -0
  22. data/lib/tabulatr/data/pagination.rb +48 -0
  23. data/lib/tabulatr/data/proxy.rb +41 -0
  24. data/lib/tabulatr/{tabulatr/batch_actions.rb → data/sorting.rb} +20 -23
  25. data/lib/tabulatr/engine.rb +24 -1
  26. data/lib/tabulatr/generators/railtie.rb +33 -0
  27. data/lib/tabulatr/generators/resource_override.rb +35 -0
  28. data/lib/{generators → tabulatr/generators}/tabulatr/install_generator.rb +21 -3
  29. data/lib/{generators → tabulatr/generators}/tabulatr/templates/tabulatr.yml +3 -1
  30. data/lib/tabulatr/generators/tabulatr/templates/tabulatr_data.rb +18 -0
  31. data/lib/tabulatr/json_builder.rb +94 -0
  32. data/lib/tabulatr/rails/action_controller.rb +36 -0
  33. data/lib/{initializers → tabulatr/rails}/action_view.rb +6 -3
  34. data/lib/{initializers → tabulatr/rails}/active_record.rb +11 -19
  35. data/lib/tabulatr/renderer/action.rb +32 -0
  36. data/lib/tabulatr/{tabulatr/formattr.rb → renderer/association.rb} +19 -30
  37. data/lib/tabulatr/renderer/checkbox.rb +36 -0
  38. data/lib/tabulatr/renderer/column.rb +113 -0
  39. data/lib/{initializers/mark_as_localizable.rb → tabulatr/renderer/columns.rb} +13 -15
  40. data/lib/tabulatr/renderer/columns_from_block.rb +56 -0
  41. data/lib/tabulatr/renderer/renderer.rb +96 -0
  42. data/lib/tabulatr/utility/utility.rb +46 -0
  43. data/lib/tabulatr/version.rb +25 -2
  44. data/lib/tabulatr.rb +45 -4
  45. data/lib/tabulatr2.rb +1 -0
  46. data/spec/dummy/app/assets/stylesheets/application.css.scss +2 -2
  47. data/spec/dummy/app/controllers/products_controller.rb +27 -3
  48. data/spec/dummy/app/models/vendor.rb +1 -0
  49. data/spec/dummy/app/tabulatr_data/product_tabulatr_data.rb +29 -0
  50. data/spec/dummy/app/views/products/implicit_columns.html.erb +1 -0
  51. data/spec/dummy/app/views/products/one_item_per_page.html.erb +1 -1
  52. data/spec/dummy/app/views/products/simple_index.html.erb +6 -4
  53. data/spec/dummy/app/views/products/stupid_array.html.erb +1 -1
  54. data/spec/dummy/app/views/products/with_batch_actions.html.erb +10 -0
  55. data/spec/dummy/config/locales/tabulatr.yml +22 -1
  56. data/spec/dummy/config/routes.rb +3 -1
  57. data/spec/features/tabulatrs_spec.rb +53 -11
  58. data/tabulatr.gemspec +1 -1
  59. metadata +50 -42
  60. data/app/assets/images/tabulatr/buttons_lite_background.png +0 -0
  61. data/app/assets/images/tabulatr/pager_arrow_left.gif +0 -0
  62. data/app/assets/images/tabulatr/pager_arrow_left_off.gif +0 -0
  63. data/app/assets/images/tabulatr/pager_arrow_right.gif +0 -0
  64. data/app/assets/images/tabulatr/pager_arrow_right_off.gif +0 -0
  65. data/app/assets/images/tabulatr/sort_arrow_down.gif +0 -0
  66. data/app/assets/images/tabulatr/sort_arrow_down_off.gif +0 -0
  67. data/app/assets/images/tabulatr/sort_arrow_up.gif +0 -0
  68. data/app/assets/images/tabulatr/sort_arrow_up_off.gif +0 -0
  69. data/app/assets/stylesheets/tabulatr/application.css +0 -40
  70. data/lib/initializers/action_controller.rb +0 -13
  71. data/lib/tabulatr/tabulatr/adapter/active_record.rb +0 -84
  72. data/lib/tabulatr/tabulatr/adapter.rb +0 -55
  73. data/lib/tabulatr/tabulatr/data_cell.rb +0 -132
  74. data/lib/tabulatr/tabulatr/dummy_record.rb +0 -40
  75. data/lib/tabulatr/tabulatr/empty_cell.rb +0 -44
  76. data/lib/tabulatr/tabulatr/filter_cell.rb +0 -145
  77. data/lib/tabulatr/tabulatr/filter_icon.rb +0 -6
  78. data/lib/tabulatr/tabulatr/finder/find_for_table.rb +0 -187
  79. data/lib/tabulatr/tabulatr/finder.rb +0 -64
  80. data/lib/tabulatr/tabulatr/header_cell.rb +0 -146
  81. data/lib/tabulatr/tabulatr/json_builder.rb +0 -57
  82. data/lib/tabulatr/tabulatr/paginator.rb +0 -76
  83. data/lib/tabulatr/tabulatr/row_builder.rb +0 -128
  84. data/lib/tabulatr/tabulatr/security.rb +0 -21
  85. data/lib/tabulatr/tabulatr/settings.rb +0 -158
  86. data/lib/tabulatr/tabulatr.rb +0 -343
  87. data/spec/lib/tabulatr/tabulatr/finder/find_for_table_spec.rb +0 -187
  88. /data/lib/{generators → tabulatr/generators}/tabulatr/templates/tabulatr.rb +0 -0
@@ -0,0 +1,135 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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
+ class Tabulatr::Data
25
+
26
+ def initialize(relation)
27
+ @relation = relation
28
+ @base = relation.respond_to?(:klass) ? relation.klass : relation
29
+ @table_name = @base.table_name
30
+ @assocs = self.class.instance_variable_get('@assocs') || HashWithIndifferentAccess.new
31
+ @columns = self.class.instance_variable_get('@columns') || HashWithIndifferentAccess.new
32
+ @search = self.class.instance_variable_get('@search') || HashWithIndifferentAccess.new
33
+ @includes = Set.new()
34
+ @cname = @base.name.downcase
35
+ @batch_actions = nil
36
+ table_columns.klass = @base
37
+ table_columns.map do |col|
38
+ col.klass = @base
39
+ end
40
+ end
41
+
42
+ def data_for_table(params, &block)
43
+
44
+ @batch_actions = block if block_given?
45
+
46
+ execute_batch_actions(batch_params(params), check_params(params))
47
+ # prepare the query
48
+ apply_filters(filter_params params)
49
+ apply_search(search_param params)
50
+ apply_sorting(sort_params params)
51
+ join_required_tables(params)
52
+
53
+ pagination = compute_pagination(params[:page], params[:pagesize])
54
+ apply_pagination(pagination)
55
+
56
+ # TODO: batch actions and checked ids
57
+
58
+ # get the records
59
+ found = apply_formats()
60
+
61
+ append = params[:append].present? ? Tabulatr::Utility.string_to_boolean(params[:append]) : false
62
+
63
+ total = @relation.unscope(:where, :limit, :offset).count
64
+
65
+ # prepare result for rendering
66
+ found.define_singleton_method(:__pagination) do
67
+ { :page => pagination[:page],
68
+ :pagesize => pagination[:pagesize],
69
+ :count => pagination[:count],
70
+ :pages => pagination[:pages],
71
+ :total => total,
72
+ :append => append,
73
+ :table_id => params[:table_id]
74
+ }
75
+ end
76
+
77
+
78
+ found.define_singleton_method(:to_tabulatr_json) do |klass=nil|
79
+ Tabulatr::JsonBuilder.build found, klass, params[:arguments]
80
+ end
81
+
82
+ found
83
+ end
84
+
85
+ def execute_batch_actions batch_param, selected_ids
86
+ if batch_param.present? && @batch_actions.present?
87
+ batch_param = batch_param.keys.first.to_sym if batch_param.is_a?(Hash)
88
+ @batch_actions.yield(Invoker.new(batch_param, selected_ids))
89
+ end
90
+ end
91
+
92
+ def table_columns
93
+ self.class.instance_variable_get("@table_columns")
94
+ end
95
+
96
+ #--
97
+ # Params
98
+ #++
99
+
100
+ def filter_params(params) params["#{@cname}_filter"] end
101
+ def search_param(params) params["#{@cname}_search"] end
102
+ def sort_params(params) params["#{@cname}_sort"] end
103
+ def batch_params(params) params["#{@cname}_batch"] end
104
+ def check_params(params)
105
+ tabulatr_checked = params["tabulatr_checked"]
106
+ if tabulatr_checked.present?
107
+ tabulatr_checked['checked_ids'].split(',')
108
+ end
109
+ end
110
+
111
+ def join_required_tables(params)
112
+ tt = (params[:arguments].split(",").select{|s| s[':']}.map do |s|
113
+ s.split(':').first
114
+ end.uniq.map(&:to_sym))
115
+ @includes = @includes + tt
116
+ # @relation = @relation.includes(@includes.map(&:to_sym)).references(@includes.map(&:to_sym))
117
+ @relation = @relation.eager_load(@includes.map(&:to_sym))
118
+ # @relation = @relation.group("#{@table_name}.#{@base.primary_key}")
119
+ end
120
+
121
+ def table_name_for_association(assoc)
122
+ @base.reflect_on_association(assoc.to_sym).table_name
123
+ # assoc.to_sym
124
+ end
125
+
126
+ end
127
+
128
+ require_relative './column_name_builder'
129
+ require_relative './dsl'
130
+ require_relative './filtering'
131
+ require_relative './invoker'
132
+ require_relative './sorting'
133
+ require_relative './pagination'
134
+ require_relative './formatting'
135
+ require_relative './proxy'
@@ -0,0 +1,61 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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 Tabulatr::Data::DSL
25
+
26
+ def column(name, sort_sql: nil, filter_sql: nil, sql: nil, table_column_options: {}, &block)
27
+ @columns ||= HashWithIndifferentAccess.new
28
+ @columns[name.to_sym] = {
29
+ name: name,
30
+ sort_sql: sort_sql || sql,
31
+ filter_sql: filter_sql || sql,
32
+ output: block
33
+ }
34
+
35
+ @table_columns ||= Tabulatr::Renderer::Columns.new(nil)
36
+ @table_columns << Tabulatr::Renderer::Column.from(table_column_options.merge(name: name))
37
+ end
38
+
39
+ def association(assoc, name, sort_sql: nil, filter_sql: nil, sql: nil, table_column_options: {}, &block)
40
+ @table_columns ||= Tabulatr::Renderer::Columns.new
41
+ @assocs ||= HashWithIndifferentAccess.new
42
+ @assocs[assoc.to_sym] ||= {}
43
+ @assocs[assoc.to_sym][name.to_sym] = {
44
+ name: name,
45
+ sort_sql: sort_sql || sql,
46
+ filter_sql: filter_sql || sql,
47
+ output: block
48
+ }
49
+
50
+ @table_columns ||= Tabulatr::Renderer::Columns.new(nil)
51
+ @table_columns << Tabulatr::Renderer::Association.from(table_column_options.merge(name: name, table_name: assoc))
52
+ end
53
+
54
+ def search(*args, &block)
55
+ raise "either column or block" if args.present? && block_given?
56
+ @search = args.presence || block
57
+ end
58
+
59
+ end
60
+
61
+ Tabulatr::Data.send :extend, Tabulatr::Data::DSL
@@ -0,0 +1,101 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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 Tabulatr::Data::Filtering
25
+
26
+ def apply_search(query)
27
+ like ||= Tabulatr::Utility.like_statement
28
+ return unless query.present?
29
+ if @search.is_a? Array
30
+ query = query.strip.gsub(/['*%\s]+/, '%')
31
+ a = @search.map do |name|
32
+ nn = build_column_name name, use_for: :filter
33
+ "(#{nn} #{like} '%#{query}%')"
34
+ end
35
+ a = a.join(' OR ')
36
+ @relation = @relation.where(a)
37
+ else # search is a proc
38
+ @relation = @relation.where(@search.(query))
39
+ end
40
+ end
41
+
42
+ def apply_filters(filter_params)
43
+ return unless filter_params
44
+ assoc_filters = filter_params.delete :__association
45
+ apply_association_filters(assoc_filters) if assoc_filters.present?
46
+ filter_params.each do |filter|
47
+ name, value = filter
48
+ next unless value.present?
49
+ nn = build_column_name name, use_for: :filter
50
+ apply_condition(nn, value)
51
+ end
52
+ end
53
+
54
+ def apply_association_filters(assoc_filters)
55
+ assoc_filters.each do |assoc_filter|
56
+ name, value = assoc_filter
57
+ assoc, att = name.split(".").map(&:to_sym)
58
+ table_name = table_name_for_association(assoc)
59
+ nn = build_column_name(att, table_name: table_name, assoc_name: assoc, use_for: :filter)
60
+ apply_condition(nn, value)
61
+ end
62
+ end
63
+
64
+ def apply_condition(n,v)
65
+ like ||= Tabulatr::Utility.like_statement
66
+ if v.is_a?(String)
67
+ @relation = @relation.where("#{n} = ?", v) unless v.blank?
68
+ 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
77
+ else
78
+ raise "Wrong filter type: #{v.class}"
79
+ end
80
+ end
81
+
82
+ def apply_date_condition(n, cond)
83
+ today = Date.today
84
+ case cond[:simple]
85
+ when 'none' then return
86
+ when 'today' then since = today
87
+ when 'yesterday' then since = today - 1.day
88
+ when 'this_week' then since = today.at_beginning_of_week
89
+ when 'last_7_days' then since = today - 7.day
90
+ when 'this_month' then since = today.at_beginning_of_month
91
+ when 'last_30_days' then since = today. - 30.day
92
+ when 'from_to'
93
+ since = Date.parse(cond[:from]) if cond[:from].present?
94
+ @relation = @relation.where("#{n} <= ?", Date.parse(cond[:to])) if cond[:to].present?
95
+ end
96
+ @relation = @relation.where("#{n} >= ?", since) if since.present?
97
+ end
98
+
99
+ end
100
+
101
+ Tabulatr::Data.send :include, Tabulatr::Data::Filtering
@@ -0,0 +1,65 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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 Tabulatr::Data::Formatting
25
+
26
+ def apply_formats()
27
+ view = Data::Proxy.new
28
+ return @relation.map do |record|
29
+ view.record = record
30
+ h = HashWithIndifferentAccess.new
31
+ @columns.each do |name, opts|
32
+ h[name] = format_column(record, name, opts, view)
33
+ end # @columns each
34
+ @assocs.each do |table_name, columns|
35
+ h[table_name] ||= {}
36
+ columns.each do |name, opts|
37
+ h[table_name][name] = format_association(record, table_name, name, opts, view)
38
+ end
39
+ end # @assocs each
40
+ h
41
+ end # @relation map
42
+ end # apply_formats
43
+
44
+ def format_column(record, name, opts, view)
45
+ if opts[:output]
46
+ view.instance_exec(record, &opts[:output])
47
+ else
48
+ record.send(name)
49
+ end
50
+ end
51
+
52
+ def format_association(record, table_name, name, opts, view)
53
+ return view.instance_exec(record, &opts[:output]) if opts[:output]
54
+ assoc = record.class.reflect_on_association(table_name.to_sym)
55
+ val = if assoc.collection?
56
+ record.try(table_name).try(:map, &name).join(', ')
57
+ else
58
+ record.try(table_name).try(:send, name)
59
+ end
60
+ end
61
+
62
+
63
+ end
64
+
65
+ Tabulatr::Data.send :include, Tabulatr::Data::Formatting
@@ -0,0 +1,37 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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
+ class Tabulatr::Data::Invoker
25
+ def initialize(batch_action, ids)
26
+ @batch_action = batch_action.to_sym
27
+ @ids = ids
28
+ end
29
+
30
+ def method_missing(name, *args, &block)
31
+ if @batch_action == name
32
+ yield(@ids)
33
+ end
34
+ end
35
+ end
36
+
37
+
@@ -0,0 +1,48 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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 Tabulatr::Data::Pagination
25
+
26
+ def apply_pagination(offset: 0, pagesize: nil, pages: nil, page: 1, count: nil)
27
+ @relation = @relation.limit(pagesize).offset(offset)
28
+ end
29
+
30
+ def compute_pagination(page, pagesize)
31
+ count = @relation.count
32
+ page ||= 1
33
+ pagesize, page = pagesize.to_i, page.to_i
34
+
35
+ pages = (count/pagesize.to_f).ceil
36
+ page = [page, pages].min
37
+
38
+ {
39
+ offset: [0,((page-1)*pagesize).to_i].max,
40
+ pagesize: pagesize,
41
+ pages: pages,
42
+ page: page,
43
+ count: count
44
+ }
45
+ end
46
+ end
47
+
48
+ Tabulatr::Data.send :include, Tabulatr::Data::Pagination
@@ -0,0 +1,41 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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
+ class Data::Proxy < ActionView::Base
25
+
26
+ attr_accessor :record
27
+
28
+ def initialize(record=nil)
29
+ self.class._init
30
+ @record = record
31
+ end
32
+
33
+ def self._init
34
+ return if @_initialized
35
+ @_initialized = true
36
+ include ActionView::Helpers
37
+ include Rails.application.helpers
38
+ include Rails.application.routes.url_helpers
39
+ end
40
+
41
+ end
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2010-2011 Peter Horn, Provideal GmbH
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
5
5
  # a copy of this software and associated documentation files (the
@@ -21,31 +21,28 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  #++
23
23
 
24
- class Tabulatr
24
+ module Tabulatr::Data::Sorting
25
25
 
26
- # render the select tag or the buttons for batch actions
27
- def render_batch_actions
28
- iname = "#{@classname}#{TABLE_FORM_OPTIONS[:batch_postfix]}"
29
- make_tag(:span, :class => 'dropdown') do
30
- make_tag(:button,
31
- :class => 'disabled btn btn-small tabulatr-wrench',
32
- :'data-toggle' => "dropdown") do
33
- concat("<i class='icon-wrench'></i>#{I18n.t('tabulatr.batch_actions')}
34
- <span class='caret'></span>")
35
- end
36
- make_tag(:ul, class: 'dropdown-menu', role: 'menu',
37
- :'aria-labelledby' => 'dLabel') do
38
- @table_options[:batch_actions].each do |n,v|
39
- make_tag(:li) do
40
- make_tag(:a, :value => v,
41
- :name => "#{iname}[#{n}]",
42
- :class => "btn batch-action-inputs") do
43
- concat(v)
44
- end
45
- end
46
- end
26
+ def apply_sorting(sortparam, default_order=nil)
27
+ if sortparam.present?
28
+ sort_by, orientation = sortparam.split(' ')
29
+ klass = sort_by.split('.').first
30
+ col_name = sort_by.split('.').last
31
+ assoc_name = nil
32
+ if klass == @cname
33
+ table_name = @base.table_name
34
+ else
35
+ assoc_name = @base.reflect_on_association(klass.to_sym).try(:name)
36
+ table_name = @base.reflect_on_association(klass.to_sym).try(:table_name)
47
37
  end
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)
40
+ @relation = @relation.order("#{nn} #{orientation}")
41
+ else
42
+ @relation = @relation.order(default_order || "#{@table_name}.#{@base.primary_key} asc")
48
43
  end
49
44
  end
50
45
 
51
46
  end
47
+
48
+ Tabulatr::Data.send :include, Tabulatr::Data::Sorting
@@ -1,3 +1,26 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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
+
1
24
  class Tabulatr::Engine < Rails::Engine
2
25
 
3
- end
26
+ end
@@ -0,0 +1,33 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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 ActiveModel
25
+ class Railtie < Rails::Railtie
26
+ initializer 'generators' do |app|
27
+ require 'rails/generators'
28
+ require 'tabulatr/generators/tabulatr/install_generator'
29
+ Rails::Generators.configure!(app.config.generators)
30
+ require 'tabulatr/generators/resource_override'
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ #--
2
+ # Copyright (c) 2010-2014 Peter Horn & Florian Thomas, Provideal GmbH
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
+ require 'rails/generators'
25
+ require 'rails/generators/rails/resource/resource_generator'
26
+
27
+ module Rails
28
+ module Generators
29
+ class ResourceGenerator
30
+ def add_tabulatr_data
31
+ invoke 'tabulatr:install'
32
+ end
33
+ end
34
+ end
35
+ end