it-logica-application-backbone 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "it-logica-application-backbone"
8
- s.version = "1.0.0"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ladas"]
12
- s.date = "2012-07-23"
12
+ s.date = "2012-07-24"
13
13
  s.description = "longer description of your gem"
14
14
  s.email = "ladislav.smola@it-logica.cz"
15
15
  s.extra_rdoc_files = [
@@ -466,13 +466,18 @@ Gem::Specification.new do |s|
466
466
  "lib/backbone_js/engine.rb",
467
467
  "lib/controller_mixins/csv_instance_methods.rb",
468
468
  "lib/controller_mixins/renderer_instance_methods.rb",
469
+ "lib/controller_mixins/table_settings_interface.rb",
469
470
  "lib/initializers/initialize.rb",
470
471
  "lib/it-logica-application-backbone.rb",
471
472
  "lib/model_mixins/ladas_html_entities.rb",
472
473
  "lib/model_mixins/ladas_string_extensions.rb",
473
474
  "lib/model_mixins/table_builder_class_methods.rb",
475
+ "lib/model_mixins/table_settings_interface.rb",
474
476
  "lib/model_mixins/tree_node_class_methods.rb",
475
477
  "lib/model_mixins/tree_node_instance_methods.rb",
478
+ "lib/table_settings.rb",
479
+ "lib/table_settings/table_action.rb",
480
+ "lib/table_settings/table_column.rb",
476
481
  "lib/view_mixins/breadcrumb.rb",
477
482
  "lib/view_mixins/datafiles_for.rb",
478
483
  "lib/view_mixins/form.rb",
@@ -0,0 +1,7 @@
1
+ module ControllerMixins
2
+ module TableSettingsInterface
3
+
4
+ require "./lib/table_settings.rb"
5
+
6
+ end
7
+ end
@@ -9,12 +9,15 @@ require 'model_mixins/table_builder_class_methods'
9
9
  require 'model_mixins/tree_node_class_methods'
10
10
  require 'model_mixins/tree_node_instance_methods'
11
11
 
12
+ require 'model_mixins/table_settings_interface'
13
+
12
14
  require "model_mixins/ladas_string_extensions"
13
15
  require "model_mixins/ladas_html_entities"
14
16
 
15
17
 
16
18
  require 'controller_mixins/renderer_instance_methods'
17
19
  require 'controller_mixins/csv_instance_methods'
20
+ require 'controller_mixins/table_settings_interface'
18
21
 
19
22
  module Initializers
20
23
  class Initialize < Rails::Railtie
@@ -27,8 +30,11 @@ module Initializers
27
30
 
28
31
  ActionController::Base.send :include, ControllerMixins::RendererInstanceMethods
29
32
  ActionController::Base.send :include, ControllerMixins::CsvInstanceMethods
33
+ ActionController::Base.send :include, ControllerMixins::TableSettingsInterface
30
34
 
31
35
  ActiveRecord::Base.send :extend, ModelMixins::TableBuilderClassMethods
36
+ ActiveRecord::Base.send :extend, ModelMixins::TableSettingsInterface
37
+
32
38
 
33
39
  String.send :include, ModelMixins::LadasStringExtensions
34
40
  String.send :include, ModelMixins::LadasHtmlEntities
@@ -0,0 +1,9 @@
1
+ module ModelMixins
2
+ module TableSettingsInterface
3
+ def table_settings
4
+ settings = TableSettings.new(self)
5
+ yield(settings) if block_given?
6
+ settings
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,269 @@
1
+ require './lib/table_settings/table_column.rb'
2
+ require './lib/table_settings/table_action.rb'
3
+
4
+ class TableSettings
5
+
6
+ attr_reader :errors
7
+
8
+ ##
9
+ # Inicializace
10
+ #
11
+ # @param [Symbol] model
12
+ #
13
+ def initialize(model)
14
+ @settings = {:columns => [], :row => {}, :default => {}}
15
+ @model = model
16
+ @default_table = table_name_from_model(model)
17
+
18
+ @column_index = 0
19
+ @columns = []
20
+ @actions = []
21
+ @errors = {}
22
+
23
+ add_defaults
24
+ end
25
+
26
+ ##
27
+ # Vysledny hash pro renderovani tabulky
28
+ #
29
+ # @return [Hash]
30
+ def hash
31
+ construct_columns if @settings[:columns].empty?
32
+ construct_actions if @settings[:row][:functions].blank?
33
+
34
+ @settings
35
+ end
36
+
37
+ def refresh_settings
38
+ @settings[:columns] = []
39
+ construct_settings
40
+ end
41
+
42
+ ##
43
+ # Prida standardni sloupec do tabulky
44
+ #
45
+ # @params [String] name - nazev sloupce v db
46
+ # @params [String] label - nazev sloupce pro zobrazeni
47
+ # @params [String] table - nazev db tabulky (nepovinne, default z konstruktoru)
48
+ #
49
+ # @return [TableSettings::Column]
50
+ #
51
+ def add_column( name,
52
+ label = nil,
53
+ table = @default_table)
54
+
55
+ column = ::TableSettings::StandardColumn.new(self, @column_index)
56
+
57
+ label = default_label(name) if label.nil?
58
+ column.params( name, label, table)
59
+
60
+ yield(column) if block_given?
61
+
62
+ @columns << column
63
+ @column_index += 1
64
+
65
+ column
66
+ end
67
+
68
+ ##
69
+ # Prida custom sloupec do tabulky
70
+ #
71
+ # @params [String] name - nazev sloupce v db
72
+ # @params [String] label - nazev sloupce pro view
73
+ # @params [String] column_method - metoda pro definováni sloupce
74
+ # @params [String] column_class - třída, ve které se volá column_method (nepovinne, defaultne vychozi model)
75
+ # @params [String] column_params - vlastni parametry pro column_method
76
+ #
77
+ # @return [TableSettings::Column]
78
+ #
79
+ def add_custom_column( name,
80
+ label,
81
+ column_method,
82
+ column_class = nil,
83
+ column_params = nil
84
+
85
+ )
86
+ column = ::TableSettings::CustomColumn.new(self, @column_index)
87
+
88
+
89
+ label = default_label(name) if label.nil?
90
+ column.params( name,
91
+ label,
92
+ column_method,
93
+ column_class,
94
+ column_params
95
+ )
96
+ yield(column) if block_given?
97
+
98
+ @columns << column
99
+ @column_index += 1
100
+
101
+ column
102
+ end
103
+
104
+ ##
105
+ # Prida akci/tlacitko/link do tabulky
106
+ #
107
+ # @params [Symbol] name - nazev akce (libovolny, mel by odpovidat akci)
108
+ # @params [String] label - nazev akce pro view
109
+ #
110
+ # @return [TableSettings::Action]
111
+ def add_action(name, label)
112
+ action = ::TableSettings::Action.new(self)
113
+
114
+ action.name = name
115
+ action.label(label)
116
+
117
+ yield(action) if block_given?
118
+
119
+ @actions << action
120
+
121
+ action
122
+ end
123
+
124
+
125
+ def form_id(id = "unique_form_id")
126
+ @settings[:form_id] = id
127
+
128
+ self
129
+ end
130
+
131
+ def row_id(row_name = "id", table_name = @default_table)
132
+ @settings[:row][:id] = table_name.to_s+"."+row_name.to_s
133
+
134
+ self
135
+ end
136
+
137
+ def order_by(row_name = "id", table_name = @default_table)
138
+ @settings[:default][:order_by] = table_name.to_s+"."+row_name.to_s
139
+
140
+ self
141
+ end
142
+
143
+ def order_by_direction(direction = "asc")
144
+ @settings[:default][:order_by_direction] = direction
145
+
146
+ self
147
+ end
148
+
149
+ def page(number = 1)
150
+ @settings[:default][:page] = number
151
+
152
+ self
153
+ end
154
+
155
+ def filter_path(path)
156
+ @settings[:filter_path] = path
157
+
158
+ self
159
+ end
160
+
161
+ def includes(options)
162
+ @settings[:includes] = options
163
+
164
+ self
165
+ end
166
+
167
+ def construct_columns
168
+ @columns.each do |column|
169
+ @settings[:columns] << column.column_hash
170
+ end
171
+ end
172
+
173
+ def construct_actions
174
+ actions = {}
175
+ @actions.each do |action|
176
+ actions[action.name] = action.action_hash
177
+ end
178
+ @settings[:row][:functions] = actions
179
+ end
180
+
181
+
182
+ def table_name_from_model(model)
183
+ if model.kind_of?(ActiveRecord::Relation)
184
+ model.klass.table_name
185
+ else
186
+ model.table_name
187
+ end
188
+ end
189
+
190
+ def add_defaults
191
+ form_id(@default_table+"_form_id")
192
+ row_id()
193
+ order_by()
194
+ order_by_direction()
195
+ page()
196
+
197
+ end
198
+
199
+ def default_label(name)
200
+ @model.human_attribute_name(name)
201
+ end
202
+
203
+
204
+ def has_includes?
205
+ @settings.has_key? :includes
206
+ end
207
+
208
+ def has_filter_path?
209
+ @settings.has_key? :filter_path
210
+ end
211
+
212
+
213
+ def has_form_id?
214
+ @settings.has_key? :form_id
215
+ end
216
+
217
+
218
+ def has_row_id?
219
+ @settings[:row].has_key? :id
220
+ end
221
+
222
+ def has_defaults?
223
+ default = @settings[:default]
224
+ default.has_key? :order_by
225
+ default.has_key? :order_by_direction
226
+ default.has_key? :page
227
+ end
228
+
229
+ def has_order_by?
230
+ @settings[:default].has_key? :order_by
231
+ end
232
+
233
+
234
+ def has_order_by_direction?
235
+ @settings[:default].has_key? :order_by_direction
236
+ end
237
+
238
+ def has_page?
239
+ @settings[:default].has_key? :page
240
+ end
241
+
242
+
243
+ def has_columns?
244
+ !@columns.empty?
245
+ end
246
+
247
+ def settings_ok?
248
+ has_filter_path? && has_form_id? && has_row_id? && has_defaults? && has_columns?
249
+ end
250
+
251
+ def set_error(type)
252
+ @errors[type.to_sym] = [type.to_s + " " + I18n.t("errors.messages.blank")]
253
+ end
254
+
255
+ def valid?
256
+ filled = nil
257
+ filled = set_error(:filter_path) unless has_filter_path?
258
+ filled = set_error(:form_id) unless has_form_id?
259
+ filled = set_error(:row_id) unless has_row_id?
260
+ filled = set_error(:order_by) unless has_order_by?
261
+ filled = set_error(:order_by_direction) unless has_order_by_direction?
262
+ filled = set_error(:page) unless has_page?
263
+ filled = set_error(:columns) unless has_columns?
264
+
265
+ filled.nil?
266
+ end
267
+
268
+
269
+ end
@@ -0,0 +1,58 @@
1
+ class TableSettings
2
+ class Action
3
+ attr_accessor :action_hash, :name
4
+
5
+ def initialize(table_settings)
6
+ @table_settings = table_settings
7
+ @action_hash = {}
8
+
9
+ add_defaults
10
+ end
11
+
12
+ def label(label)
13
+ @action_hash[:name] = label
14
+ self
15
+ end
16
+
17
+ def add_defaults
18
+ @action_hash[:symlink_remote] = true
19
+ self
20
+ end
21
+
22
+ def controller(name)
23
+ @action_hash[:symlink_controller] = name
24
+ self
25
+ end
26
+
27
+ def action(name)
28
+ @action_hash[:symlink_action] = name
29
+ self
30
+ end
31
+ def outer_controller(name)
32
+ @action_hash[:symlink_outer_controller] = name
33
+ self
34
+ end
35
+ def outer_id(name)
36
+ @action_hash[:symlink_outer_id] = name
37
+ self
38
+ end
39
+
40
+ def remote(bool)
41
+ @action_hash[:symlink_remote] = bool
42
+ self
43
+ end
44
+
45
+ def http_method(name)
46
+ @action_hash[:method] = name
47
+ self
48
+ end
49
+
50
+ def css_class(name)
51
+ @action_hash[:class] = name
52
+ self
53
+ end
54
+
55
+
56
+
57
+ end
58
+ end
@@ -0,0 +1,91 @@
1
+
2
+ class TableSettings
3
+ attr_accessor :table_settings
4
+
5
+ class Column
6
+ attr_accessor :column_hash, :index
7
+
8
+ def initialize(table_settings, index)
9
+ @table_settings = table_settings
10
+ @column_hash = {}
11
+ @index = index
12
+ end
13
+
14
+
15
+ def css_class(class_name)
16
+ @column_hash[:class_name] = class_name
17
+
18
+ self
19
+ end
20
+
21
+ def filter_type(filter_type)
22
+ if filter_type == :none
23
+ @column_hash.delete(:filter)
24
+ else
25
+ @column_hash[:filter] = filter_type
26
+ end
27
+
28
+ self
29
+ end
30
+
31
+ def filter_data(array)
32
+ @column_hash[:filter_data] = array
33
+
34
+ self
35
+ end
36
+
37
+ def max_text_length(length)
38
+ @column_hash[:max_text_length] = length
39
+
40
+ self
41
+ end
42
+
43
+ def format_method(method_name)
44
+ @column_hash[:format_method] = method_name
45
+
46
+ self
47
+ end
48
+
49
+ def global_format_method(method_name)
50
+ @column_hash[:global_format_method] = method_name
51
+
52
+ self
53
+ end
54
+
55
+
56
+ end
57
+
58
+ class CustomColumn < Column
59
+
60
+
61
+ def params( name, label, column_method, column_class = nil, column_params = nil)
62
+
63
+ @column_hash = {
64
+ :name => name,
65
+ :label => label,
66
+ :column_method => column_method,
67
+ :filter => :find
68
+ }
69
+ @column_hash[:column_class] = column_class unless column_class.nil?
70
+ @column_hash[:column_params] = column_params unless column_params.nil?
71
+
72
+ self
73
+ end
74
+ end
75
+
76
+ class StandardColumn < Column
77
+
78
+ def params(name, label, table)
79
+
80
+ @column_hash = {
81
+ :name => name,
82
+ :label => label,
83
+ :table => table,
84
+ :filter => :find
85
+ }
86
+
87
+ self
88
+ end
89
+ end
90
+
91
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: it-logica-application-backbone
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-23 00:00:00.000000000 Z
12
+ date: 2012-07-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: htmlentities
16
- requirement: &73084770 !ruby/object:Gem::Requirement
16
+ requirement: &73834790 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *73084770
24
+ version_requirements: *73834790
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: shoulda
27
- requirement: &73084290 !ruby/object:Gem::Requirement
27
+ requirement: &73834310 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *73084290
35
+ version_requirements: *73834310
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rdoc
38
- requirement: &73083670 !ruby/object:Gem::Requirement
38
+ requirement: &73833830 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3.12'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *73083670
46
+ version_requirements: *73833830
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &73082850 !ruby/object:Gem::Requirement
49
+ requirement: &73833470 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.1.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *73082850
57
+ version_requirements: *73833470
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: jeweler
60
- requirement: &73082290 !ruby/object:Gem::Requirement
60
+ requirement: &73833140 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.8.4
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *73082290
68
+ version_requirements: *73833140
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
- requirement: &73081790 !ruby/object:Gem::Requirement
71
+ requirement: &73832750 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *73081790
79
+ version_requirements: *73832750
80
80
  description: longer description of your gem
81
81
  email: ladislav.smola@it-logica.cz
82
82
  executables: []
@@ -534,13 +534,18 @@ files:
534
534
  - lib/backbone_js/engine.rb
535
535
  - lib/controller_mixins/csv_instance_methods.rb
536
536
  - lib/controller_mixins/renderer_instance_methods.rb
537
+ - lib/controller_mixins/table_settings_interface.rb
537
538
  - lib/initializers/initialize.rb
538
539
  - lib/it-logica-application-backbone.rb
539
540
  - lib/model_mixins/ladas_html_entities.rb
540
541
  - lib/model_mixins/ladas_string_extensions.rb
541
542
  - lib/model_mixins/table_builder_class_methods.rb
543
+ - lib/model_mixins/table_settings_interface.rb
542
544
  - lib/model_mixins/tree_node_class_methods.rb
543
545
  - lib/model_mixins/tree_node_instance_methods.rb
546
+ - lib/table_settings.rb
547
+ - lib/table_settings/table_action.rb
548
+ - lib/table_settings/table_column.rb
544
549
  - lib/view_mixins/breadcrumb.rb
545
550
  - lib/view_mixins/datafiles_for.rb
546
551
  - lib/view_mixins/form.rb
@@ -563,7 +568,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
563
568
  version: '0'
564
569
  segments:
565
570
  - 0
566
- hash: 619515891
571
+ hash: -806299903
567
572
  required_rubygems_version: !ruby/object:Gem::Requirement
568
573
  none: false
569
574
  requirements: