active_list 6.5.1 → 6.6.0
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.
- checksums.yaml +4 -4
- data/lib/active_list.rb +11 -11
- data/lib/active_list/definition.rb +0 -2
- data/lib/active_list/definition/abstract_column.rb +4 -9
- data/lib/active_list/definition/action_column.rb +49 -57
- data/lib/active_list/definition/association_column.rb +26 -28
- data/lib/active_list/definition/attribute_column.rb +6 -11
- data/lib/active_list/definition/check_box_column.rb +0 -5
- data/lib/active_list/definition/data_column.rb +17 -24
- data/lib/active_list/definition/empty_column.rb +0 -4
- data/lib/active_list/definition/field_column.rb +0 -4
- data/lib/active_list/definition/status_column.rb +0 -4
- data/lib/active_list/definition/table.rb +31 -37
- data/lib/active_list/definition/text_field_column.rb +0 -4
- data/lib/active_list/exporters.rb +0 -4
- data/lib/active_list/exporters/abstract_exporter.rb +15 -19
- data/lib/active_list/exporters/csv_exporter.rb +5 -11
- data/lib/active_list/exporters/excel_csv_exporter.rb +7 -13
- data/lib/active_list/exporters/open_document_spreadsheet_exporter.rb +29 -35
- data/lib/active_list/generator.rb +27 -32
- data/lib/active_list/generator/finder.rb +35 -50
- data/lib/active_list/helpers.rb +6 -8
- data/lib/active_list/rails/engine.rb +3 -3
- data/lib/active_list/rails/integration.rb +2 -10
- data/lib/active_list/renderers.rb +0 -4
- data/lib/active_list/renderers/abstract_renderer.rb +2 -7
- data/lib/active_list/renderers/simple_renderer.rb +58 -71
- data/lib/active_list/version.rb +1 -3
- data/test/active_list_test.rb +4 -6
- data/test/code_generation_test.rb +0 -2
- data/test/dummy/Gemfile +1 -2
- data/test/dummy/app/controllers/contacts_controller.rb +10 -10
- data/test/dummy/app/controllers/people_controller.rb +10 -11
- data/test/dummy/config.ru +1 -1
- data/test/dummy/config/application.rb +2 -2
- data/test/dummy/config/boot.rb +1 -1
- data/test/dummy/config/environments/test.rb +6 -3
- data/test/dummy/config/initializers/session_store.rb +1 -1
- data/test/dummy/config/initializers/wrap_parameters.rb +1 -1
- data/test/dummy/db/seeds.rb +5 -5
- data/test/dummy/script/rails +2 -2
- data/test/people_controller_test.rb +11 -15
- data/test/table_test.rb +0 -1
- data/test/test_helper.rb +6 -7
- metadata +3 -4
@@ -2,22 +2,19 @@
|
|
2
2
|
require 'zip'
|
3
3
|
|
4
4
|
# Register ODS format unless is already set
|
5
|
-
Mime::Type.register(
|
5
|
+
Mime::Type.register('application/vnd.oasis.opendocument.spreadsheet', :ods) unless defined? Mime::ODS
|
6
6
|
|
7
7
|
module ActiveList
|
8
|
-
|
9
8
|
module Exporters
|
10
|
-
|
11
9
|
class OpenDocumentSpreadsheetExporter < AbstractExporter
|
12
|
-
|
13
10
|
DATE_ELEMENTS = {
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
'm' => "<number:month number:style=\"long\"/>",
|
12
|
+
'd' => "<number:day number:style=\"long\"/>",
|
13
|
+
'Y' => '<number:year/>'
|
17
14
|
}
|
18
15
|
|
19
16
|
def file_extension
|
20
|
-
|
17
|
+
'ods'
|
21
18
|
end
|
22
19
|
|
23
20
|
def mime_type
|
@@ -27,56 +24,53 @@ module ActiveList
|
|
27
24
|
def send_data_code
|
28
25
|
xml_escape = "to_s.gsub('&', '&').gsub('\\'', ''').gsub('<', '<').gsub('>', '>')"
|
29
26
|
xml_escape << ".force_encoding('US-ASCII')" if xml_escape.respond_to?(:force_encoding)
|
30
|
-
record =
|
31
|
-
code
|
27
|
+
record = 'r'
|
28
|
+
code = generator.select_data_code(paginate: false)
|
32
29
|
code << "name = #{table.model.name}.model_name.human.gsub(/[^a-z0-9]/i, '_')\n"
|
33
|
-
code << "file = ActiveList.temporary_directory.join(name + rand(999_999_999).to_s(36) + '.#{
|
30
|
+
code << "file = ActiveList.temporary_directory.join(name + rand(999_999_999).to_s(36) + '.#{file_extension}')\n"
|
34
31
|
code << "FileUtils.mkdir_p(file.dirname)\n"
|
35
32
|
code << "Zip::OutputStream.open(file) do |zile|\n"
|
36
33
|
# MimeType in first place
|
37
34
|
code << " zile.put_next_entry('mimetype', nil, nil, Zip::Entry::STORED)\n"
|
38
|
-
code << " zile << '#{
|
35
|
+
code << " zile << '#{mime_type}'\n"
|
39
36
|
|
40
37
|
# Manifest
|
41
38
|
code << " zile.put_next_entry('META-INF/manifest.xml')\n"
|
42
|
-
code << " zile << ('<?xml version=\"1.0\" encoding=\"UTF-8\"?><manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\"><manifest:file-entry manifest:media-type=\"#{
|
39
|
+
code << " zile << ('<?xml version=\"1.0\" encoding=\"UTF-8\"?><manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\"><manifest:file-entry manifest:media-type=\"#{mime_type}\" manifest:full-path=\"/\"/><manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/></manifest:manifest>')\n"
|
43
40
|
code << " zile.put_next_entry('content.xml')\n"
|
44
41
|
|
45
42
|
code << " zile << ('<?xml version=\"1.0\" encoding=\"UTF-8\"?><office:document-content xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\" xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\" xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\" xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\" xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\" xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\" xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\" xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\" xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\" xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\" xmlns:math=\"http://www.w3.org/1998/Math/MathML\" xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\" xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\" xmlns:ooo=\"http://openoffice.org/2004/office\" xmlns:ooow=\"http://openoffice.org/2004/writer\" xmlns:oooc=\"http://openoffice.org/2004/calc\" xmlns:dom=\"http://www.w3.org/2001/xml-events\" xmlns:xforms=\"http://www.w3.org/2002/xforms\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:field=\"urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:field:1.0\" office:version=\"1.1\"><office:scripts/>')\n"
|
46
43
|
# Styles
|
47
|
-
code << " zile << ('<office:automatic-styles>"
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
code << " zile << ('<office:automatic-styles>"\
|
45
|
+
"<style:style style:name=\"co1\" style:family=\"table-column\"><style:table-column-properties fo:break-before=\"auto\" style:use-optimal-column-width=\"true\"/></style:style>"\
|
46
|
+
"<style:style style:name=\"header\" style:family=\"table-cell\"><style:text-properties fo:font-weight=\"bold\" style:font-weight-asian=\"bold\" style:font-weight-complex=\"bold\"/></style:style>"\
|
47
|
+
"<number:date-style style:name=\"K4D\" number:automatic-order=\"true\"><number:text>" + ::I18n.translate('date.formats.default').gsub(/\%./) { |x| '</number:text>' + DATE_ELEMENTS[x[1..1]] + '<number:text>' } + "</number:text></number:date-style><style:style style:name=\"ce1\" style:family=\"table-cell\" style:data-style-name=\"K4D\"/>"\
|
48
|
+
"</office:automatic-styles>')\n"
|
52
49
|
|
53
50
|
# Tables
|
54
51
|
code << " zile << ('<office:body><office:spreadsheet><table:table table:name=\"'+#{table.model.name}.model_name.human.#{xml_escape}+'\">')\n"
|
55
52
|
code << " zile << ('<table:table-column table:number-columns-repeated=\"#{table.exportable_columns.size}\"/>')\n"
|
56
|
-
code << " zile << ('<table:table-header-rows><table:table-row>"+columns_headers.collect{|h| "<table:table-cell table:style-name=\"header\" office:value-type=\"string\"><text:p>'+(#{h}).#{xml_escape}+'</text:p></table:table-cell>"}.join+"</table:table-row></table:table-header-rows>')\n"
|
53
|
+
code << " zile << ('<table:table-header-rows><table:table-row>" + columns_headers.collect { |h| "<table:table-cell table:style-name=\"header\" office:value-type=\"string\"><text:p>'+(#{h}).#{xml_escape}+'</text:p></table:table-cell>" }.join + "</table:table-row></table:table-header-rows>')\n"
|
57
54
|
code << " for #{record} in #{generator.records_variable_name}\n"
|
58
|
-
code << " zile << ('<table:table-row>"+table.exportable_columns.collect do |column|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
end.join+"</table:table-row>')\n"
|
55
|
+
code << " zile << ('<table:table-row>" + table.exportable_columns.collect do |column|
|
56
|
+
'<table:table-cell' + (if column.numeric? || column.datatype == :decimal
|
57
|
+
" office:value-type=\"float\" office:value=\"'+(#{column.datum_code(record)}).#{xml_escape}+'\""
|
58
|
+
elsif column.datatype == :boolean
|
59
|
+
" office:value-type=\"boolean\" office:boolean-value=\"'+(#{column.datum_code(record)}).#{xml_escape}+'\""
|
60
|
+
elsif column.datatype == :date
|
61
|
+
" office:value-type=\"date\" table:style-name=\"ce1\" office:date-value=\"'+(#{column.datum_code(record)}).#{xml_escape}+'\""
|
62
|
+
else
|
63
|
+
" office:value-type=\"string\""
|
64
|
+
end) + "><text:p>'+(" + column.exporting_datum_code(record, true) + ").#{xml_escape}+'</text:p></table:table-cell>"
|
65
|
+
end.join + "</table:table-row>')\n"
|
69
66
|
code << " end\n"
|
70
67
|
code << " zile << ('</table:table></office:spreadsheet></office:body></office:document-content>')\n"
|
71
68
|
code << "end\n"
|
72
|
-
code << "send_file(file, stream: false, type: #{
|
69
|
+
code << "send_file(file, stream: false, type: #{mime_type.to_s.inspect}, disposition: 'inline', filename: name+'.#{file_extension}')\n"
|
73
70
|
# code << "File.delete(file)\n" # Removes tmp files before they explode the disk
|
74
71
|
# raise code
|
75
|
-
|
72
|
+
code
|
76
73
|
end
|
77
|
-
|
78
74
|
end
|
79
|
-
|
80
75
|
end
|
81
|
-
|
82
76
|
end
|
@@ -1,17 +1,14 @@
|
|
1
|
-
# encoding: UTF-8
|
2
1
|
module ActiveList
|
3
|
-
|
4
2
|
class Generator
|
5
|
-
|
6
3
|
attr_accessor :table, :controller, :controller_method_name, :view_method_name, :records_variable_name
|
7
4
|
|
8
|
-
def initialize(*args, &
|
5
|
+
def initialize(*args, &_block)
|
9
6
|
options = args.extract_options!
|
10
7
|
@controller = options[:controller]
|
11
8
|
name = args.shift || @controller.controller_name.to_sym
|
12
|
-
model = (options[:model]||name).to_s.classify.constantize
|
9
|
+
model = (options[:model] || name).to_s.classify.constantize
|
13
10
|
@collection = !!(model.name == @controller.controller_name.to_s.classify)
|
14
|
-
@controller_method_name = "list#{'_'+name.to_s if name != @controller.controller_name.to_sym}"
|
11
|
+
@controller_method_name = "list#{'_' + name.to_s if name != @controller.controller_name.to_sym}"
|
15
12
|
@view_method_name = "_#{@controller.controller_name}_list_#{name}_tag"
|
16
13
|
@records_variable_name = "@#{name}"
|
17
14
|
@table = ActiveList::Definition::Table.new(name, model, options)
|
@@ -20,7 +17,7 @@ module ActiveList
|
|
20
17
|
else
|
21
18
|
@table.load_default_columns
|
22
19
|
end
|
23
|
-
@parameters = {sort: :to_s, dir: :to_s}
|
20
|
+
@parameters = { sort: :to_s, dir: :to_s }
|
24
21
|
@parameters.merge!(page: :to_i, per_page: :to_i) if @table.paginate?
|
25
22
|
end
|
26
23
|
|
@@ -37,15 +34,15 @@ module ActiveList
|
|
37
34
|
end
|
38
35
|
|
39
36
|
def controller_method_code
|
40
|
-
code
|
41
|
-
code << "def #{
|
42
|
-
code <<
|
37
|
+
code = "# encoding: utf-8\n"
|
38
|
+
code << "def #{controller_method_name}\n"
|
39
|
+
code << session_initialization_code.dig
|
43
40
|
code << " respond_to do |format|\n"
|
44
41
|
code << " format.html do\n"
|
45
42
|
code << " if request.xhr?\n"
|
46
|
-
code <<
|
43
|
+
code << renderer.remote_update_code.dig(4)
|
47
44
|
code << " else\n"
|
48
|
-
code << " render(inline: '<%=#{
|
45
|
+
code << " render(inline: '<%=#{view_method_name}-%>')\n" # , layout: action_has_layout?
|
49
46
|
code << " end\n"
|
50
47
|
code << " end\n"
|
51
48
|
for format, exporter in ActiveList::Exporters.hash
|
@@ -55,40 +52,40 @@ module ActiveList
|
|
55
52
|
end
|
56
53
|
code << " end\n"
|
57
54
|
# Save preferences of user
|
58
|
-
if defined?
|
59
|
-
code << " p = current_user.
|
55
|
+
if defined?(User) && User.instance_methods.include?(:preference)
|
56
|
+
code << " p = current_user.preference('list.#{view_method_name}', YAML::dump({}))\n"
|
60
57
|
code << " p.set! YAML::dump(#{var_name(:params)}.stringify_keys)\n"
|
61
58
|
end
|
62
59
|
code << "end\n"
|
63
60
|
# code.split("\n").each_with_index{|l, x| puts((x+1).to_s.rjust(4)+": "+l)}
|
64
61
|
unless ::Rails.env.production?
|
65
|
-
file = ::Rails.root.join(
|
62
|
+
file = ::Rails.root.join('tmp', 'code', 'active_list', 'controllers', controller.controller_path, controller_method_name + '.rb')
|
66
63
|
FileUtils.mkdir_p(file.dirname)
|
67
64
|
File.write(file, code)
|
68
65
|
end
|
69
|
-
|
66
|
+
code
|
70
67
|
end
|
71
68
|
|
72
69
|
def view_method_code
|
73
|
-
code
|
74
|
-
code << "def #{
|
75
|
-
code <<
|
76
|
-
code <<
|
70
|
+
code = "# encoding: utf-8\n"
|
71
|
+
code << "def #{view_method_name}(options={}, &block)\n"
|
72
|
+
code << session_initialization_code.dig
|
73
|
+
code << renderer.build_table_code.dig
|
77
74
|
code << "end\n"
|
78
75
|
# code.split("\n").each_with_index{|l, x| puts((x+1).to_s.rjust(4)+": "+l)}
|
79
76
|
unless ::Rails.env.production?
|
80
|
-
file = ::Rails.root.join(
|
77
|
+
file = ::Rails.root.join('tmp', 'code', 'active_list', 'views', controller.controller_path, view_method_name + '.rb')
|
81
78
|
FileUtils.mkdir_p(file.dirname)
|
82
79
|
File.write(file, code)
|
83
80
|
end
|
84
|
-
|
81
|
+
code
|
85
82
|
end
|
86
83
|
|
87
84
|
def session_initialization_code
|
88
|
-
code
|
85
|
+
code = "options = {} unless options.is_a? Hash\n"
|
89
86
|
code << "options.update(params)\n"
|
90
|
-
if defined?
|
91
|
-
code << "#{var_name(:params)} = YAML::load(current_user.
|
87
|
+
if defined?(User) && User.instance_methods.include?(:preference)
|
88
|
+
code << "#{var_name(:params)} = YAML::load(current_user.preference('list.#{view_method_name}', YAML::dump({})).value).symbolize_keys\n"
|
92
89
|
code << "#{var_name(:params)} = {} unless #{var_name(:params)}.is_a?(Hash)\n"
|
93
90
|
else
|
94
91
|
code << "#{var_name(:params)} = {}\n"
|
@@ -97,11 +94,11 @@ module ActiveList
|
|
97
94
|
code << "unless #{var_name(:params)}[:hidden_columns].is_a? Array\n"
|
98
95
|
code << " #{var_name(:params)}[:hidden_columns] = #{@table.hidden_columns.map(&:name).map(&:to_sym).inspect}\n"
|
99
96
|
code << "end\n"
|
100
|
-
for parameter, convertor in @parameters.sort{|a,b| a[0].to_s <=> b[0].to_s}
|
97
|
+
for parameter, convertor in @parameters.sort { |a, b| a[0].to_s <=> b[0].to_s }
|
101
98
|
# expr = "options.delete('#{@table.name}_#{parameter}') || options.delete('#{parameter}') || #{var_name(:params)}[:#{parameter}]"
|
102
99
|
# expr += " || #{@table.options[parameter]}" unless @table.options[parameter].blank?
|
103
100
|
# code << "#{var_name(:params)}[:#{parameter}] = (#{expr}).#{convertor}\n"
|
104
|
-
expr
|
101
|
+
expr = "#{var_name(:params)}[:#{parameter}]"
|
105
102
|
expr = "(#{expr} || #{@table.options[parameter]})" unless @table.options[parameter].blank?
|
106
103
|
code << "#{var_name(:params)}[:#{parameter}] = #{expr}.#{convertor}\n"
|
107
104
|
end
|
@@ -109,16 +106,14 @@ module ActiveList
|
|
109
106
|
|
110
107
|
# Order
|
111
108
|
code << "#{var_name(:order)} = #{@table.options[:order] ? @table.options[:order].inspect : 'nil'}\n"
|
112
|
-
code << "if #{var_name(:col)} = {" + @table.sortable_columns.collect{|c| "'#{c.sort_id}' => '#{c.sort_expression}'"}.join(', ') + "}[#{var_name(:params)}[:sort]]\n"
|
109
|
+
code << "if #{var_name(:col)} = {" + @table.sortable_columns.collect { |c| "'#{c.sort_id}' => '#{c.sort_expression}'" }.join(', ') + "}[#{var_name(:params)}[:sort]]\n"
|
113
110
|
code << " #{var_name(:params)}[:dir] = 'asc' unless #{var_name(:params)}[:dir] == 'asc' or #{var_name(:params)}[:dir] == 'desc'\n"
|
114
111
|
code << " #{var_name(:order)} = #{var_name(:col)} + ' ' + #{var_name(:params)}[:dir]\n"
|
115
112
|
code << "end\n"
|
116
113
|
|
117
|
-
|
114
|
+
code
|
118
115
|
end
|
119
|
-
|
120
116
|
end
|
121
|
-
|
122
117
|
end
|
123
118
|
|
124
|
-
require
|
119
|
+
require 'active_list/generator/finder'
|
@@ -1,15 +1,13 @@
|
|
1
1
|
module ActiveList
|
2
|
-
|
3
2
|
# Manage data query
|
4
3
|
class Generator
|
5
|
-
|
6
4
|
# Generate select code for the table taking all parameters in account
|
7
5
|
def select_data_code(options = {})
|
8
|
-
paginate = (options.
|
6
|
+
paginate = (options.key?(:paginate) ? options[:paginate] : @table.paginate?)
|
9
7
|
# Check order
|
10
8
|
unless @table.options.keys.include?(:order)
|
11
9
|
columns = @table.table_columns
|
12
|
-
@table.options[:order] = (columns.any? ? columns.first.name.to_sym : {id: :desc})
|
10
|
+
@table.options[:order] = (columns.any? ? columns.first.name.to_sym : { id: :desc })
|
13
11
|
end
|
14
12
|
|
15
13
|
class_name = @table.model.name
|
@@ -17,17 +15,17 @@ module ActiveList
|
|
17
15
|
|
18
16
|
# Find data
|
19
17
|
query_code = "#{class_name}"
|
20
|
-
query_code <<
|
21
|
-
query_code << ".select(#{
|
22
|
-
query_code << ".where(#{
|
18
|
+
query_code << scope_code if scope_code
|
19
|
+
query_code << ".select(#{select_code})" if select_code
|
20
|
+
query_code << ".where(#{conditions_code})" unless @table.options[:conditions].blank?
|
23
21
|
query_code << ".joins(#{@table.options[:joins].inspect})" unless @table.options[:joins].blank?
|
24
|
-
unless
|
25
|
-
expr =
|
22
|
+
unless includes_reflections.empty?
|
23
|
+
expr = includes_reflections.inspect[1..-2]
|
26
24
|
query_code << ".includes(#{expr})"
|
27
25
|
query_code << ".references(#{expr})"
|
28
26
|
end
|
29
27
|
|
30
|
-
code
|
28
|
+
code = ''
|
31
29
|
code << "#{var_name(:count)} = #{query_code}.count\n"
|
32
30
|
|
33
31
|
query_code << ".reorder(#{var_name(:order)})"
|
@@ -48,16 +46,15 @@ module ActiveList
|
|
48
46
|
code << "#{var_name(:offset)} = (#{var_name(:page)} - 1) * #{var_name(:limit)}\n"
|
49
47
|
code << "#{var_name(:last)} = (#{var_name(:count)}.to_f / #{var_name(:limit)}).ceil.to_i\n"
|
50
48
|
code << "#{var_name(:last)} = 1 if #{var_name(:last)} < 1\n"
|
51
|
-
|
52
49
|
|
53
|
-
code << "return #{
|
54
|
-
code << "return #{
|
50
|
+
code << "return #{view_method_name}(options.merge(page: 1)) if 1 > #{var_name(:page)}\n"
|
51
|
+
code << "return #{view_method_name}(options.merge(page: #{var_name(:last)})) if #{var_name(:page)} > #{var_name(:last)}\n"
|
55
52
|
query_code << ".offset(#{var_name(:offset)})"
|
56
53
|
query_code << ".limit(#{var_name(:limit)})"
|
57
54
|
end
|
58
55
|
|
59
|
-
code << "#{
|
60
|
-
|
56
|
+
code << "#{records_variable_name} = #{query_code} || {}\n"
|
57
|
+
code
|
61
58
|
end
|
62
59
|
|
63
60
|
protected
|
@@ -65,25 +62,21 @@ module ActiveList
|
|
65
62
|
# Compute includes Hash
|
66
63
|
def includes_reflections
|
67
64
|
hash = []
|
68
|
-
|
69
|
-
if column.respond_to?(:reflection)
|
70
|
-
hash << column.reflection.name
|
71
|
-
end
|
65
|
+
@table.columns.each do |column|
|
66
|
+
hash << column.reflection.name if column.respond_to?(:reflection)
|
72
67
|
end
|
73
|
-
|
68
|
+
hash
|
74
69
|
end
|
75
70
|
|
76
|
-
|
77
71
|
def scope_code
|
78
72
|
return nil unless scopes = @table.options[:scope]
|
79
73
|
scopes = [scopes].flatten
|
80
|
-
code
|
81
|
-
|
74
|
+
code = ''
|
75
|
+
scopes.each do |scope|
|
82
76
|
code << ".#{scope}"
|
83
77
|
end
|
84
|
-
|
78
|
+
code
|
85
79
|
end
|
86
|
-
|
87
80
|
|
88
81
|
# Generate the code from a conditions option
|
89
82
|
def conditions_code
|
@@ -92,44 +85,43 @@ module ActiveList
|
|
92
85
|
case conditions
|
93
86
|
when Array
|
94
87
|
case conditions[0]
|
95
|
-
when String
|
88
|
+
when String # SQL
|
96
89
|
code << '[' + conditions.first.inspect
|
97
|
-
code << conditions[1..-1].collect{|p|
|
90
|
+
code << conditions[1..-1].collect { |p| ', ' + sanitize_condition(p) }.join if conditions.size > 1
|
98
91
|
code << ']'
|
99
92
|
when Symbol # Method
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
93
|
+
fail 'What?'
|
94
|
+
# code << conditions.first.to_s + '('
|
95
|
+
# code << conditions[1..-1].collect { |p| sanitize_condition(p) }.join(', ') if conditions.size > 1
|
96
|
+
# code << ')'
|
104
97
|
else
|
105
|
-
|
98
|
+
fail ArgumentError, 'First element of an Array can only be String or Symbol.'
|
106
99
|
end
|
107
100
|
when Hash # SQL
|
108
|
-
code << '{' + conditions.collect{|key, value| key.to_s + ': ' + sanitize_condition(value)}.join(',') + '}'
|
101
|
+
code << '{' + conditions.collect { |key, value| key.to_s + ': ' + sanitize_condition(value) }.join(',') + '}'
|
109
102
|
when Symbol # Method
|
110
|
-
code << conditions.to_s +
|
103
|
+
code << conditions.to_s + '(options)'
|
111
104
|
when CodeString
|
112
|
-
code <<
|
105
|
+
code << '(' + conditions.gsub(/\s*\n\s*/, ';') + ')'
|
113
106
|
when String
|
114
107
|
code << conditions.inspect
|
115
108
|
else
|
116
|
-
|
109
|
+
fail ArgumentError, "Unsupported type for conditions: #{conditions.inspect}"
|
117
110
|
end
|
118
|
-
|
111
|
+
code
|
119
112
|
end
|
120
113
|
|
121
114
|
def select_code
|
122
|
-
return nil unless @table.options[:distinct]
|
123
|
-
code
|
124
|
-
code <<
|
115
|
+
return nil unless @table.options[:distinct] || @table.options[:select]
|
116
|
+
code = ''
|
117
|
+
code << 'DISTINCT ' if @table.options[:distinct]
|
125
118
|
code << "#{@table.model.table_name}.*"
|
126
119
|
if @table.options[:select]
|
127
|
-
code << @table.options[:select].collect{|k, v| ", #{k[0].to_s+'.'+k[1].to_s} AS #{v}" }.join
|
120
|
+
code << @table.options[:select].collect { |k, v| ", #{k[0].to_s + '.' + k[1].to_s} AS #{v}" }.join
|
128
121
|
end
|
129
|
-
|
122
|
+
("'" + code + "'").c
|
130
123
|
end
|
131
124
|
|
132
|
-
|
133
125
|
def sanitize_condition(value)
|
134
126
|
# if value.is_a? Array
|
135
127
|
# # if value.size==1 and value[0].is_a? String
|
@@ -150,12 +142,5 @@ module ActiveList
|
|
150
142
|
value.inspect
|
151
143
|
end
|
152
144
|
end
|
153
|
-
|
154
|
-
|
155
145
|
end
|
156
|
-
|
157
|
-
|
158
146
|
end
|
159
|
-
|
160
|
-
|
161
|
-
|
data/lib/active_list/helpers.rb
CHANGED
@@ -1,21 +1,20 @@
|
|
1
1
|
module ActiveList
|
2
2
|
module Helpers
|
3
|
-
|
4
3
|
def recordify!(value, record_name)
|
5
4
|
if value.is_a?(Symbol)
|
6
|
-
return record_name +
|
5
|
+
return record_name + '.' + value.to_s
|
7
6
|
elsif value.is_a?(CodeString)
|
8
|
-
return
|
7
|
+
return '(' + value.gsub(/RECORD/, record_name) + ')'
|
9
8
|
else
|
10
|
-
|
9
|
+
fail ArgumentError, 'CodeString or Symbol must be given to be recordified)'
|
11
10
|
end
|
12
11
|
end
|
13
12
|
|
14
13
|
def recordify(value, record_name)
|
15
14
|
if value.is_a?(Symbol)
|
16
|
-
return record_name +
|
15
|
+
return record_name + '.' + value.to_s
|
17
16
|
elsif value.is_a?(CodeString)
|
18
|
-
return
|
17
|
+
return '(' + value.gsub(/RECORD/, record_name) + ')'
|
19
18
|
else
|
20
19
|
return value.inspect
|
21
20
|
end
|
@@ -23,11 +22,10 @@ module ActiveList
|
|
23
22
|
|
24
23
|
def urlify(value, record_name)
|
25
24
|
if value.is_a?(CodeString)
|
26
|
-
return
|
25
|
+
return '(' + value.gsub(/RECORD/, record_name) + ')'
|
27
26
|
else
|
28
27
|
return value.inspect
|
29
28
|
end
|
30
29
|
end
|
31
|
-
|
32
30
|
end
|
33
31
|
end
|