From 051e0dcd77b674b86f68059bed2decd81851f5b5 Mon Sep 17 00:00:00 2001 From: Tim Felgentreff Date: Fri, 28 Jan 2011 14:33:00 +0100 Subject: [PATCH 1/5] accept a block on render methods to specify enclosed content --- lib/widget.rb | 11 ++++++++--- lib/widget/base.rb | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/widget.rb b/lib/widget.rb index 6becfe4609..47ff6ac885 100644 --- a/lib/widget.rb +++ b/lib/widget.rb @@ -1,10 +1,11 @@ class ActionView::Base - def render_widget(widget, subject, options = nil) + def render_widget(widget, subject, options = {}, &block) i = widget.new(subject) i.config = config i.controller = controller i._content_for = @_content_for - (options ? i.render_with_options(options) : i.render).html_safe + i._routes = _routes + i.render_with_options(options, &block).html_safe end end @@ -14,7 +15,7 @@ class Widget < ActionView::Base include ActionView::Helpers::FormTagHelper include ActionView::Helpers::JavaScriptHelper - attr_accessor :output_buffer, :controller, :config, :_content_for + attr_accessor :output_buffer, :controller, :config, :_content_for, :_routes extend ProactiveAutoloader @@ -25,4 +26,8 @@ class Widget < ActionView::Base def current_language ::I18n.locale end + + def protect_against_forgery? + false + end end diff --git a/lib/widget/base.rb b/lib/widget/base.rb index 35a27f560e..b44567b4d6 100644 --- a/lib/widget/base.rb +++ b/lib/widget/base.rb @@ -10,11 +10,11 @@ class Widget::Base < Widget raise NotImplementedError, "#render is missing in my subclass" end - def render_with_options(options = {}) + def render_with_options(options = {}, &block) if canvas = options[:to] - canvas << "\n" << render + canvas << "\n" << render(&block) else - raise ArgumentError, "invalid options" + render(&block) end end end From c778e2d9b124d2f0c70042cb9fbc0ae3ae4d29b7 Mon Sep 17 00:00:00 2001 From: Tim Felgentreff Date: Fri, 28 Jan 2011 14:33:19 +0100 Subject: [PATCH 2/5] fix filters typo --- lib/widget/filters.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/widget/filters.rb b/lib/widget/filters.rb index c0410cc47e..06da5d0fcd 100644 --- a/lib/widget/filters.rb +++ b/lib/widget/filters.rb @@ -35,7 +35,7 @@ class Widget::Filters < Widget::Base def render_filters active_filters = @query.filters.select {|f| f.class.display? } engine::Filter.all.collect do |filter| - opts = :id => "tr_#{filter.underscore_name}", :class => "#{filter.underscore_name}" + opts = {:id => "tr_#{filter.underscore_name}", :class => "#{filter.underscore_name}"} active_instance = active_filters.detect {|f| f.class == filter } if active_instance opts[:"data-selected"] = true From 09bf0b70cf7b45e046adba6e3a84d18be4b4aeb8 Mon Sep 17 00:00:00 2001 From: Tim Felgentreff Date: Fri, 28 Jan 2011 14:33:45 +0100 Subject: [PATCH 3/5] pre-render selected operator and multi-values --- lib/widget/filters/multi_values.rb | 6 +++--- lib/widget/filters/operators.rb | 12 +++--------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/widget/filters/multi_values.rb b/lib/widget/filters/multi_values.rb index 3d94eb1cea..649919c786 100644 --- a/lib/widget/filters/multi_values.rb +++ b/lib/widget/filters/multi_values.rb @@ -16,9 +16,9 @@ class Widget::Filters::MultiValues < Widget::Filters::Base name = l(name) if name.is_a? Symbol name_prefix = ((level && level > 0) ? (' ' * 2 * level + '> ') : '') unless options[:optgroup] - content_tag :option, :value => id do - name_prefix + h(name) - end + opts = { :value => id } + opts[:selected] = "selected" if filter.values.include? id + content_tag(:option, opts) { name_prefix + h(name) } else tag :optgroup, :label => l(:label_sector) end diff --git a/lib/widget/filters/operators.rb b/lib/widget/filters/operators.rb index 99d9a3e3e7..e26af55e98 100644 --- a/lib/widget/filters/operators.rb +++ b/lib/widget/filters/operators.rb @@ -7,15 +7,9 @@ class Widget::Filters::Operators < Widget::Filters::Base :onchange => "operator_changed('#{filter_class.underscore_name}', this);", :name => "operators[#{filter_class.underscore_name}]" do filter_class.available_operators.collect do |o| - if filter.operator.to_s == o.to_s - content_tag :option, :value => h(o.to_s), :"data-arity" => o.arity, :selected => "selected" do - h(l(o.label)) - end - else - content_tag :option, :value => h(o.to_s), :"data-arity" => o.arity do - h(l(o.label)) - end - end + opts = {:value => h(o.to_s), :"data-arity" => o.arity} + opts[:selected] = "selected" if filter.operator.to_s == o.to_s + content_tag(:option, opts) { h(l(o.label)) } end.join.html_safe end end From 82fded35ab946cec069a60911270159d4ef9c509 Mon Sep 17 00:00:00 2001 From: Tim Felgentreff Date: Fri, 28 Jan 2011 14:34:19 +0100 Subject: [PATCH 4/5] create a settings widget to render filter and group by settings as one --- lib/widget/settings.rb | 33 +++++++++++++++++++++++++++++++++ lib/widget/settings/fieldset.rb | 15 +++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 lib/widget/settings/fieldset.rb diff --git a/lib/widget/settings.rb b/lib/widget/settings.rb index 463f876996..2a5e820c92 100644 --- a/lib/widget/settings.rb +++ b/lib/widget/settings.rb @@ -1,2 +1,35 @@ class Widget::Settings < Widget::Base + def render + form_for @query, :url => "#", :html => {:id => 'query_form', :method => :post} do |query_form| + content_tag :div, :id => "query_form_content" do + + fieldsets = render_widget Widget::Settings::Fieldset, :type => "filter" do + render_widget Widget::Filters, @query + end + + fieldsets += render_widget Widget::Settings::Fieldset, :type => "group_by" do + render_widget Widget::GroupBys, @query + end + + buttons = content_tag :p, :class => "buttons form_controls" do + p = link_to({}, {:href => "#", + :onclick => " + selectAllOptions('group_by_rows'); + selectAllOptions('group_by_columns'); + new Ajax.Updater('result-table', + '#{url_for(:action => 'index', :set_filter => '1')}', + { asynchronous:true, + evalScripts:true, + postBody: Form.serialize('query_form') + '\\n' + $('filters').innerHTML }); + return false;".html_safe, + :class => 'button apply'}) do + content_tag(:span, content_tag(:em, l(:button_apply))) + end + p += link_to_function l(:button_reset), "restore_query_inputs();", :class => 'icon icon-reload' + end + + fieldsets + buttons + end + end + end end diff --git a/lib/widget/settings/fieldset.rb b/lib/widget/settings/fieldset.rb new file mode 100644 index 0000000000..c4f71d2d22 --- /dev/null +++ b/lib/widget/settings/fieldset.rb @@ -0,0 +1,15 @@ +class Widget::Settings::Fieldset < Widget::Base + def render_with_options(options, &block) + @type = options.delete(:type) || "filter" + @id = "#{@type}-settings" + @label = :"label_#{@type}" + super(options, &block) + end + + def render + content_tag :fieldset, :id => @id, :class => "collapsible collapsed" do + html = content_tag :legend, l(@label), :onclick => "toggleFieldset(this);" #FIXME: onclick + html + yield + end + end +end From 1ecc7e48e6409c93c46cedae463d0eee5d567835 Mon Sep 17 00:00:00 2001 From: Tim Felgentreff Date: Fri, 28 Jan 2011 15:00:35 +0100 Subject: [PATCH 5/5] stub table widget --- lib/widget/table.rb | 20 ++++++++++++++++++++ lib/widget/table/detailed_table.rb | 2 ++ lib/widget/table/report_table.rb | 5 +++++ 3 files changed, 27 insertions(+) create mode 100644 lib/widget/table/detailed_table.rb create mode 100644 lib/widget/table/report_table.rb diff --git a/lib/widget/table.rb b/lib/widget/table.rb index 10f68eecb9..dc8e58c7cb 100644 --- a/lib/widget/table.rb +++ b/lib/widget/table.rb @@ -1,2 +1,22 @@ class Widget::Table < Widget::Base + def render + if @query.group_bys.empty? + widget = Widget::Table::DetailedTable + else + if @query.depth_of(:column) == 0 + @query.column(:singleton_value) + elsif @query.depth_of(:row) == 0 + @query.row(:singleton_value) + end + widget = Widget::Table::ReportTable + end + + content_tag :div, :id => "result-table" do + if @query.result.count > 0 + render_widget widget, @query + else + content_tag :p, l(:label_no_data), :class => "nodata" + end + end + end end diff --git a/lib/widget/table/detailed_table.rb b/lib/widget/table/detailed_table.rb new file mode 100644 index 0000000000..0185758c9e --- /dev/null +++ b/lib/widget/table/detailed_table.rb @@ -0,0 +1,2 @@ +class Widget::Table::DetailedTable < Widget::Base +end diff --git a/lib/widget/table/report_table.rb b/lib/widget/table/report_table.rb new file mode 100644 index 0000000000..d103c84ed4 --- /dev/null +++ b/lib/widget/table/report_table.rb @@ -0,0 +1,5 @@ +class Widget::Table::ReportTable < Widget::Base + def render + "blah" + end +end