showbuilder 0.0.5 → 0.0.6

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/lib/showbuilder.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'showbuilder/corekit'
2
2
  require 'showbuilder/show_model_view'
3
3
  require 'showbuilder/show_model_form'
4
- require 'showbuilder/show_model_list'
4
+ require 'showbuilder/show_model_table'
5
5
  require 'showbuilder/show_form'
6
6
  require 'showbuilder/show_paginate_renderer'
7
7
 
@@ -10,7 +10,7 @@ module Showbuilder
10
10
  include Showbuilder::I18nText
11
11
  include Showbuilder::ShowModelView
12
12
  include Showbuilder::ShowModelForm
13
- include Showbuilder::ShowModelList
13
+ include Showbuilder::ShowModelTable
14
14
  include Showbuilder::ShowForm
15
15
 
16
16
  def show_paginate_renderer
@@ -89,4 +89,5 @@ module Showbuilder
89
89
  ::ActionView::Base.field_error_proc = proc do |html_tag, instance_tag|
90
90
  html_tag
91
91
  end
92
+
92
93
  end
@@ -26,7 +26,7 @@ module Showbuilder
26
26
 
27
27
  def show_email_input(method)
28
28
  self.show_method_input(method) do
29
- self.contents_tag(:div, :class => "input-append") do |contents|
29
+ self.contents_tag(:div, :class => "input-append") do |contents|
30
30
  contents << self.email_field(method, :class => :medium)
31
31
  contents << self.content_tag(:span, content_tag(:i, '', :class =>"icon-envelope"), :class => "add-on")
32
32
  end
@@ -44,7 +44,7 @@ module Showbuilder
44
44
  self.text_area(method, :rows => 5)
45
45
  end
46
46
  end
47
-
47
+
48
48
  def show_select_input(method, choices, lable, options = {}, html_options = {})
49
49
  self.show_method_input(method, :label_text => lable) do
50
50
  self.select(method, choices, options, html_options)
@@ -59,7 +59,7 @@ module Showbuilder
59
59
  div_options = " error"
60
60
  end
61
61
 
62
- self.contents_tag :div, :class => "control-group #{div_options}" do |contents|
62
+ self.contents_tag :div, :class => "control-group #{div_options}" do |contents|
63
63
  contents << self.label(method, label_text, :class => "control-label")
64
64
  contents << self.content_tag(:div, :class => 'controls') do
65
65
  yield
@@ -0,0 +1,182 @@
1
+ require 'showbuilder/builders/template_methods'
2
+ require 'showbuilder/i18n_text'
3
+
4
+ module Showbuilder
5
+ module Builders
6
+ class ShowModelTableRowBuilder
7
+ include Showbuilder::Builders::TemplateMethods
8
+ include Showbuilder::I18nText
9
+
10
+ attr_accessor :is_header
11
+ attr_accessor :itext_base
12
+ attr_accessor :model
13
+
14
+ def initialize(template)
15
+ @template = template
16
+ end
17
+
18
+ # show_text_column :number
19
+ # show_text_column :sale, :number
20
+ def show_text_column(*methods)
21
+ return show_header_column(methods) if is_header
22
+
23
+ content = get_methods_text_value(model, methods)
24
+ content_tag :td, content
25
+ end
26
+
27
+ # show_currency_column :price
28
+ # show_currency_column :product, :price
29
+ def show_currency_column(*methods)
30
+ return show_header_column(methods) if is_header
31
+
32
+ content = get_methods_currency_value(model, methods)
33
+ content_tag :td, content
34
+ end
35
+
36
+ # show_percent_column :discount
37
+ # show_percent_column :product, :discount
38
+ def show_percent_column(*methods)
39
+ return show_header_column(methods) if is_header
40
+
41
+ content = get_methods_percent_value(model, methods)
42
+ content_tag :td, content
43
+ end
44
+
45
+ # show_text_link_column :number
46
+ # show_text_link_column :sale, :number
47
+ # show_text_link_column :sale, :number, :link => :sale
48
+ # show_text_link_column :sale, :number, :link => [:sale, :customer]
49
+ def show_text_link_column(*methods)
50
+ return show_header_column(methods) if is_header
51
+
52
+ name = get_methods_text_value(model, methods)
53
+ content_tag :td do
54
+ show_column_link name, methods
55
+ end
56
+ end
57
+
58
+ # show_date_link_column :create_at
59
+ # show_date_link_column :sale, :create_at
60
+ # show_date_link_column :sale, :create_at, :link => :sale
61
+ # show_date_link_column :sale, :create_at, :link => [:sale, :customer]
62
+ def show_date_link_column(*methods)
63
+ return show_header_column(methods) if is_header
64
+
65
+ name = get_methods_date_value(model, methods)
66
+ content_tag :td do
67
+ show_column_link name, methods
68
+ end
69
+ end
70
+
71
+ # show_time_link_column :create_at
72
+ # show_time_link_column :sale, :create_at
73
+ # show_time_link_column :sale, :create_at, :link => :sale
74
+ # show_time_link_column :sale, :create_at, :link => [:sale, :customer]
75
+ def show_time_link_column(*methods)
76
+ return show_header_column(methods) if is_header
77
+
78
+ name = get_methods_time_value(model, methods)
79
+ content_tag :td do
80
+ show_column_link name, methods
81
+ end
82
+ end
83
+
84
+ # show_currency_link_column :price
85
+ # show_currency_link_column :product, :price
86
+ # show_currency_link_column :product, :price, :link => :product
87
+ # show_currency_link_column :product, :price, :link => [:sale, :customer]
88
+ def show_currency_link_column(*methods)
89
+ return show_header_column(methods) if is_header
90
+
91
+ name = get_methods_currency_value(model, methods)
92
+ content_tag :td do
93
+ show_column_link name, methods
94
+ end
95
+ end
96
+
97
+ private
98
+
99
+ def show_header_column(methods = nil)
100
+ text = ''
101
+ if methods
102
+ methods = Array.wrap(methods)
103
+ methods = filter_methods_options(methods)
104
+ text = show_current_itext(methods)
105
+ end
106
+ content_tag :th, text
107
+ end
108
+
109
+ def show_column_link(name, methods)
110
+ link_object = get_link_object(methods)
111
+ show_model_link_to name, link_object
112
+ end
113
+
114
+ def get_link_object(methods)
115
+ if methods.count == 1
116
+ return model
117
+ end
118
+
119
+ link_methods = get_methods_option(methods, :link)
120
+ if link_methods
121
+ link_object = call_object_methods(model, link_methods)
122
+ return link_object
123
+ end
124
+
125
+ return model
126
+ end
127
+
128
+ def get_methods_text_value(model, methods)
129
+ methods = filter_methods_options(methods)
130
+ value = call_object_methods(model, methods)
131
+ value.to_s
132
+ end
133
+
134
+ def get_methods_date_value(model, methods)
135
+ methods = filter_methods_options(methods)
136
+ value = call_object_methods(model, methods)
137
+ date_string value
138
+ end
139
+
140
+ def get_methods_time_value(model, methods)
141
+ methods = filter_methods_options(methods)
142
+ value = call_object_methods(model, methods)
143
+ time_string value
144
+ end
145
+
146
+ def get_methods_currency_value(model, methods)
147
+ methods = filter_methods_options(methods)
148
+ value = call_object_methods(model, methods)
149
+ currency_string value
150
+ end
151
+
152
+ def get_methods_percent_value(model, methods)
153
+ methods = filter_methods_options(methods)
154
+ value = call_object_methods(model, methods)
155
+ percent_string value
156
+ end
157
+
158
+ def filter_methods_options(methods)
159
+ case methods.last
160
+ when Hash
161
+ return methods[0..-2]
162
+ else
163
+ return methods
164
+ end
165
+ end
166
+
167
+ def get_methods_option(methods, key)
168
+ case methods.last
169
+ when Hash
170
+ return methods.last[key]
171
+ else
172
+ return nil
173
+ end
174
+ end
175
+
176
+ def show_current_itext_base
177
+ itext_base || controller_name.to_s.singularize
178
+ end
179
+
180
+ end
181
+ end
182
+ end
@@ -1,18 +1,19 @@
1
1
  module Showbuilder
2
2
  module I18nText
3
+
3
4
  def show_current_itext(text_id, *args)
4
5
  case text_id
5
6
  when Array
6
7
  text_id = text_id.join('.')
7
8
  end
8
9
 
9
- if self.show_current_itext_base
10
- current_text_id = "#{self.show_current_itext_base}.#{text_id}"
10
+ current_text_id = if show_current_itext_base
11
+ "#{show_current_itext_base}.#{text_id}"
11
12
  else
12
- current_text_id = text_id
13
+ text_id
13
14
  end
14
15
 
15
- self.show_itext(current_text_id, *args)
16
+ show_itext current_text_id, *args
16
17
  end
17
18
 
18
19
  def show_current_itext_base
@@ -21,6 +22,7 @@ module Showbuilder
21
22
  def show_itext(id, *args)
22
23
  I18n.t(id, *args)
23
24
  end
25
+
24
26
  end
25
27
  end
26
28
 
@@ -0,0 +1,89 @@
1
+ require 'showbuilder/builders/model_table_row_builder'
2
+
3
+ module Showbuilder
4
+ module ShowModelTable
5
+
6
+ #
7
+ # = show_model_table @sale do |row|
8
+ # = row.show_date_link_column :created_at
9
+ # = row.show_text_link_column :number
10
+ # = row.show_text_link_column :customer, :name, :link => :customer
11
+ # = row.show_currency_link_column :total
12
+ #
13
+ def show_model_table(models, itext_base = nil, &block)
14
+ contents_tag(:table, :class => "table table-bordered table-striped") do |contents|
15
+ contents << show_model_table_header(itext_base, &block)
16
+ contents << show_model_table_body(models, &block)
17
+ end
18
+ end
19
+
20
+ def show_model_table_header(itext_base, &block)
21
+ content_tag :thead do
22
+ show_model_table_header_row itext_base, &block
23
+ end
24
+ end
25
+
26
+ def show_model_table_body(models, &block)
27
+ contents_tag :tbody do |contents|
28
+ models.each do |model|
29
+ contents << show_model_table_body_row(model, &block)
30
+ end
31
+ contents << show_model_table_no_record_row(block) if models.count == 0
32
+ end
33
+ end
34
+
35
+ def show_model_table_no_record_row(block)
36
+ column_count = get_show_model_table_column_count(block)
37
+ content_tag :tr do
38
+ content_tag :td, :colspan => column_count do
39
+ show_itext 'no_record'
40
+ end
41
+ end
42
+ end
43
+
44
+ def show_model_table_header_row(itext_base, &block)
45
+ row = Showbuilder::Builders::ShowModelTableRowBuilder.new(self)
46
+ row.is_header = true
47
+ row.itext_base = itext_base
48
+
49
+ content_tag :tr do
50
+ capture row, &block
51
+ end
52
+ end
53
+
54
+ def show_model_table_body_row(model, &block)
55
+ row = Showbuilder::Builders::ShowModelTableRowBuilder.new(self)
56
+ row.is_header = false
57
+ row.model = model
58
+
59
+ content_tag :tr do
60
+ capture row, &block
61
+ end
62
+ end
63
+
64
+ def get_show_model_table_column_count(block)
65
+ row = Object.new
66
+ class << row
67
+ attr_accessor :column_count
68
+
69
+ def increament_column_count
70
+ self.column_count ||= 0
71
+ self.column_count += 1
72
+ end
73
+
74
+ def method_missing(meth, *args, &block)
75
+ if meth.to_s =~ /^show_(.+)_column$/
76
+ self.increament_column_count
77
+ else
78
+ super
79
+ end
80
+ end
81
+ end
82
+
83
+ capture row, &block
84
+
85
+ return row.column_count
86
+ end
87
+
88
+ end
89
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: showbuilder
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ery Wang, Mario Du
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-12 00:00:00 Z
18
+ date: 2012-05-24 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: A Rails View Helper. Show model/s as view, form, list.
@@ -32,14 +32,13 @@ files:
32
32
  - lib/showbuilder/i18n_text.rb
33
33
  - lib/showbuilder/corekit.rb
34
34
  - lib/showbuilder/show_paginate_renderer.rb
35
- - lib/showbuilder/show_model_list.rb
36
35
  - lib/showbuilder/builders/template_methods.rb
37
- - lib/showbuilder/builders/model_list_builder.rb
38
36
  - lib/showbuilder/builders/form_builder.rb
39
37
  - lib/showbuilder/builders/model_form_builder.rb
40
38
  - lib/showbuilder/builders/model_view_builder.rb
41
- - lib/showbuilder/builders/model_list_builder/show_columns.rb
39
+ - lib/showbuilder/builders/model_table_row_builder.rb
42
40
  - lib/showbuilder/show_model_form.rb
41
+ - lib/showbuilder/show_model_table.rb
43
42
  - lib/showbuilder/show_form.rb
44
43
  homepage: https://github.com/ery/showbuilder
45
44
  licenses: []
@@ -1,122 +0,0 @@
1
- require 'showbuilder/builders/template_methods'
2
- require 'showbuilder/builders/model_list_builder/show_columns'
3
- require 'showbuilder/i18n_text'
4
-
5
- module Showbuilder
6
- module Builders
7
- class ModelListBuilder
8
- include Showbuilder::I18nText
9
- include Showbuilder::Builders::TemplateMethods
10
- include Showbuilder::Builders::ModelListBuilder::ShowColumns
11
-
12
- class Column
13
- attr_accessor :methods
14
- attr_accessor :build_body_column_method
15
- attr_accessor :build_header_column_method
16
- end
17
-
18
- attr_reader :template
19
- attr_reader :text_group
20
- attr_accessor :columns
21
- attr_accessor :table_options
22
- attr_accessor :body_row_options
23
-
24
- def initialize(template, text_group)
25
- @template = template
26
- @text_group = text_group
27
- self.columns = []
28
- end
29
-
30
- def build_model_list(models, &block)
31
- block.call(self)
32
- self.table_options ||= {}
33
- self.table_options[:class] ||= ""
34
- self.table_options[:class] += " table table-bordered table-striped"
35
-
36
- contents_tag(:table, self.table_options) do |contents|
37
- contents << self.build_header
38
- contents << self.build_body(models)
39
- end
40
- end
41
-
42
- protected
43
-
44
- def build_header
45
- self.content_tag :thead do
46
- self.contents_tag :tr do |contents|
47
- self.columns.each do |column|
48
- contents << self.build_header_column(column)
49
- end
50
- end
51
- end
52
- end
53
-
54
- def build_header_column(column)
55
- if column.build_header_column_method
56
- column.build_header_column_method.call(column)
57
- else
58
- self.content_tag(:th) do
59
- if column.methods
60
- methods = Array.wrap(column.methods)
61
- methods = filter_method_option(methods)
62
- text_id = methods.join('.')
63
- self.show_current_itext(text_id)
64
- end
65
- end
66
- end
67
- end
68
-
69
- def build_body(models)
70
- models ||= []
71
-
72
- contents_tag :tbody do |contents|
73
- if models.count == 0
74
- contents << build_body_row_for_no_record
75
- else
76
- models.each do |model|
77
- contents << build_body_row(model)
78
- end
79
- end
80
- end
81
- end
82
-
83
- def build_body_row(model)
84
- options = self.build_body_row_options
85
- contents_tag(:tr, :class => "#{options}")do |contents|
86
- self.columns.each do |column|
87
- contents << self.build_body_column(column, model)
88
- end
89
- end
90
- end
91
-
92
- def build_body_row_options
93
- even = self.cycle('', 'even')
94
- self.body_row_options ||= {}
95
- self.body_row_options[:class] ||= ""
96
- self.body_row_options[:class] += " #{even}"
97
- end
98
-
99
- def build_body_row_for_no_record
100
- contents_tag :tr do |contents|
101
- self.columns.each_index do |index|
102
- contents << self.content_tag(:td) do
103
- show_itext('no_record') if index == 0
104
- end
105
- end
106
- end
107
- end
108
-
109
- def build_body_column(column, model)
110
- if column.build_body_column_method
111
- column.build_body_column_method.call(model)
112
- else
113
- self.tag(:td)
114
- end
115
- end
116
-
117
- def show_current_itext_base
118
- @text_group || self.controller_name.to_s.singularize
119
- end
120
- end
121
- end
122
- end
@@ -1,152 +0,0 @@
1
- module Showbuilder
2
- module Builders
3
- class ModelListBuilder
4
- module ShowColumns
5
-
6
- # show_text_column :number
7
- # show_text_column :member, :number
8
- def show_text_column(*methods)
9
- self.show_method_column(methods) do |value|
10
- self.safe_html_string(value)
11
- end
12
- end
13
-
14
- # show_text_link_column :number
15
- # show_text_link_column :member, :number
16
- # show_text_link_column :member, :number, :link => :member
17
- # show_text_link_column :member, :number, :link => [:member, :department]
18
- # show_text_link_column :member, :number, :link => [:member, :department], :class => 'member_list'
19
- def show_text_link_column(*methods)
20
- self.show_method_link_column(*methods) do |value|
21
- self.safe_html_string(value)
22
- end
23
- end
24
-
25
- # show_time_column :created_at
26
- def show_time_column(*methods)
27
- self.show_method_column(methods) do |value|
28
- self.time_string(value)
29
- end
30
- end
31
-
32
- def show_time_link_column(*methods)
33
- self.show_method_link_column(*methods) do |value|
34
- self.time_string(value)
35
- end
36
- end
37
-
38
- # show_date_column :created_at
39
- def show_date_column(*methods)
40
- self.show_method_column(methods) do |value|
41
- self.date_string(value)
42
- end
43
- end
44
-
45
- # show_date_link_column :created_at
46
- def show_date_link_column(*methods)
47
- self.show_method_link_column(*methods) do |value|
48
- self.date_string(value)
49
- end
50
- end
51
-
52
- # show_currency_column :total
53
- def show_currency_column(*methods)
54
- self.show_method_column(methods) do |value|
55
- self.currency_string(value)
56
- end
57
- end
58
-
59
- def show_currency_link_column(*methods)
60
- self.show_method_link_column(*methods) do |value|
61
- self.currency_string(value)
62
- end
63
- end
64
-
65
- # show_percent_column :percent
66
- def show_percent_column(*methods)
67
- self.show_method_column(methods) do |value|
68
- self.percent_string(value)
69
- end
70
- end
71
-
72
- def show_method_column(methods, &block)
73
- self.show_column(methods) do |model|
74
- self.content_tag(:td, :class => find_option_form_methods(methods, :class)) do
75
- method_value = self.call_object_methods(model, methods)
76
- if block
77
- content = block.call(method_value)
78
- else
79
- content = method_value
80
- end
81
- content.to_s
82
- end
83
- end
84
- end
85
-
86
- def show_method_link_column(*methods, &customize_link_name_block)
87
- if methods.count == 0
88
- raise 'show_method_link_column need a argument!'
89
- end
90
-
91
- self.show_column(methods) do |model|
92
- self.content_tag(:td, :class => find_option_form_methods(methods, :class)) do
93
- link_object = self.show_method_link_column__get_link_object(model, methods)
94
- link_name = self.show_method_link_column__get_link_name(model, methods, customize_link_name_block)
95
- self.show_model_link_to link_name, link_object
96
- end
97
- end
98
- end
99
-
100
- def find_option_form_methods(methods, key)
101
- last_item = methods.last
102
- if last_item.is_a? Hash
103
- return last_item[key]
104
- else
105
- return nil
106
- end
107
- end
108
-
109
- def show_method_link_column__get_link_object(model, methods)
110
- if methods.count == 1
111
- return model
112
- end
113
-
114
- link_methods = find_option_form_methods(methods, :link)
115
- if link_methods
116
- link_object = self.call_object_methods(model, link_methods)
117
- return link_object
118
- end
119
-
120
- return model
121
- end
122
-
123
- def show_method_link_column__get_link_name(model, methods, customize_link_name_block)
124
- methods = filter_method_option(methods)
125
- method_value = self.call_object_methods(model, methods)
126
- if customize_link_name_block
127
- link_name = customize_link_name_block.call(method_value)
128
- else
129
- link_name = method_value
130
- end
131
- link_name.to_s
132
- end
133
-
134
- def show_column(methods = nil, build_header_column_method = nil, &build_body_column_method)
135
- column = ModelListBuilder::Column.new
136
- column.methods = methods
137
- column.build_header_column_method = build_header_column_method
138
- column.build_body_column_method = build_body_column_method
139
- self.columns << column
140
- end
141
-
142
- def filter_method_option(methods)
143
- if methods.last.is_a? Hash
144
- methods[0..-2]
145
- else
146
- methods
147
- end
148
- end
149
- end
150
- end
151
- end
152
- end
@@ -1,25 +0,0 @@
1
- require 'showbuilder/builders/model_list_builder'
2
-
3
- module Showbuilder
4
- module ShowModelList
5
- #
6
- # show_model_list @products do |list|
7
- # list.show_column :some_field
8
- # list.show_text_column :some_field
9
- # list.show_currency_column :some_field
10
- # list.show_percent_column :some_field
11
- # list.show_date_column :some_field
12
- # list.show_time_column :some_field
13
- # list.show_text_link_column :some_field
14
- # list.show_currency_link_column :some_field
15
- # list.show_percent_link_column :some_field
16
- # list.show_date_link_column :some_field
17
- # list.show_time_link_column :some_field
18
- # end
19
- #
20
- def show_model_list(models, text_group = nil, &block)
21
- builder = Showbuilder::Builders::ModelListBuilder.new(self, text_group)
22
- builder.build_model_list(models, &block)
23
- end
24
- end
25
- end