# encoding: UTF-8 require 'spec_helper' class MyFormBuilder include Datagrid::FormBuilder end class MyTemplate include ActionView::Helpers::FormHelper end describe Datagrid::FormBuilder do let(:template) do ActionView::Base.new.tap do |v| v.view_paths << File.expand_path("../../../app/views", __FILE__) v.view_paths << File.expand_path("../../support/test_partials", __FILE__) end end let(:view) { ActionView::Helpers::FormBuilder.new(:report, _grid, template, {}, Proc.new {|f| })} describe ".datagrid_filter" do it "should work for every filter type" do Datagrid::Filters::FILTER_TYPES.each do |type, klass| expect(Datagrid::FormBuilder.instance_methods.map(&:to_sym)).to include(klass.form_builder_helper_name) end end subject do view.datagrid_filter(_filter, _filter_options, &_filter_block) end let(:_filter_options) { {} } let(:_filter_block) { nil } context "with default filter type" do let(:_grid) { test_report do scope {Entry} filter(:name) end } let(:_filter) { :name } it { should equal_to_dom( '' )} end context "with integer filter type" do let(:_filter) { :group_id } let(:_grid) { test_report do scope {Entry} filter(:group_id, :integer) end } it { should equal_to_dom( '' )} context "when partials option is passed for filter that don't support range" do let(:_filter_options) { {partials: 'anything' } } it { should equal_to_dom( '' )} end end context "with date filter type" do let(:_filter) { :created_at } let(:_grid) { test_report do scope {Entry} filter(:created_at, :date) end } it { should equal_to_dom( '' )} context "when special date format specified" do around(:each) do |example| _grid.created_at = Date.parse('2012-01-02') with_date_format do example.run end end it { should equal_to_dom( '' )} end end context "with integer filter type and range option" do let(:_filter) { :group_id } let(:_grid) { test_report(:group_id => _range) do scope {Entry} filter(:group_id, :integer, :range => true) end } context "when datagrid_filter options has id" do let(:_filter_options) { {:id => "hello"} } let(:_range) { [1,2]} it { should equal_to_dom( '' + ' - ' + '' )} end context "with only left bound" do let(:_range) { [10, nil]} it { should equal_to_dom( '' + ' - ' + '' )} it { should be_html_safe } end context "with only right bound" do let(:_range) { [nil, 10]} it { should equal_to_dom( '' + ' - ' + '' )} it { should be_html_safe } end context "with invalid range value" do let(:_range) { 2..1 } it { should equal_to_dom( '' + ' - ' + '' )} end context "with custom partials option and template exists" do let(:_filter_options) { { :partials => 'custom_range' } } let(:_range) { nil } it { should equal_to_dom( "custom_range_partial" ) } end context "when custom partial doesn't exist" do let(:_filter_options) { { :partials => 'not_existed' } } let(:_range) { nil } it { should equal_to_dom( ' - ' ) } end context "when deprecated format translation specified" do let(:_range) { nil } around(:each) do |example| store_translations(:en, datagrid: {filters: {integer: {range_format: "from %{from_input} to %{to_input}"}}}) do silence_warnings do example.run end end end it { should equal_to_dom( 'from to ' )} end context "when deprecated separator is specified" do let(:_range) { nil } around(:each) do |example| store_translations(:en, datagrid: {filters: {integer: {range_separator: " | "}}}) do silence_warnings do example.run end end end it { should equal_to_dom( ' | ' )} end end context "with float filter type and range option" do let(:_filter) { :rating } let(:_grid) { test_report(:rating => _range) do scope {Group} filter(:rating, :float, :range => true) end } let(:_range) { [1.5,2.5]} it { should equal_to_dom( '' + ' - ' + '' )} end context "with date filter type and range option" do let(:_filter) { :created_at } let(:_grid) { test_report(:created_at => _range) do scope {Entry} filter(:created_at, :date, :range => true) end } context "with only left bound" do let(:_range) { ["2012-01-03", nil]} it { should equal_to_dom( '' + ' - ' + '' )} it { should be_html_safe } end context "when special date format specified" do around(:each) do |example| with_date_format do example.run end end let(:_range) { ["2013/01/01", '2013/02/02']} it { should equal_to_dom( '' + ' - ' + '' )} end context "with only right bound" do let(:_range) { [nil, "2012-01-03"]} it { should equal_to_dom( '' + ' - ' + '' )} it { should be_html_safe } end context "with invalid range value" do let(:_range) { Date.parse('2012-01-02')..Date.parse('2012-01-01') } it { should equal_to_dom( '' + ' - ' + '' )} end context "with blank range value" do around(:each) do |example| with_date_format do example.run end end let(:_range) { [nil, nil] } it { should equal_to_dom( '' + ' - ' + '' )} end end context "with enum filter type" do let(:_filter) { :category } let(:_grid) { test_report do scope {Entry} filter(:category, :enum, :select => ["first", "second"]) filter(:category_without_include_blank, :enum, :select => ["first", "second"], :include_blank => false) filter(:category_with_prompt, :enum, :select => ["first", "second"], :prompt => "My Prompt") end } it { should equal_to_dom( '' )} context "when block is given" do before(:each) do skip("not supported by rails < 4.1") if Rails.version < '4.1' end let(:_filter_block ) { proc { template.content_tag(:option, 'block option', :value => 'block_value') }} it { should equal_to_dom( '' )} end context "when first option is selected" do before(:each) do _grid.category = "first" end it { should equal_to_dom( '' )} end context "with include_blank option set to false" do let(:_filter) { :category_without_include_blank } it { should equal_to_dom( '' )} end context "with prompt option" do let(:_filter) { :category_with_prompt } it { should equal_to_dom( '' )} end context "with checkboxes option" do let(:_grid) do test_report do scope {Entry} filter(:category, :enum, :select => ["first", "second"], :checkboxes => true) end end let(:_filter) { :category } if Rails.version >= "4.1" it { should equal_to_dom( ' ' )} else it { should equal_to_dom( ' ' )} end context "when partials option passed and partial exists" do let(:_filter_options) { {partials: 'custom_checkboxes'} } it { should equal_to_dom('custom_enum_checkboxes') } end end end context "with boolean filter type" do let(:_filter) { :disabled } let(:_grid) do test_report do scope {Entry} filter(:disabled, :boolean, default: true) end end it { should equal_to_dom( # hidden is important when default is set to true '' )} end context "with xboolean filter type" do let(:_filter) { :disabled } let(:_grid) do test_report do scope {Entry} filter(:disabled, :xboolean) end end it { should equal_to_dom( '' )} end context "with string filter" do let(:_grid) do test_report do scope {Entry} filter(:name, :string) end end let(:_filter) { :name } it {should equal_to_dom('')} context "when multiple option is set" do let(:_grid) do test_report(:name => "one,two") do scope {Entry} filter(:name, :string, :multiple => true) end end let(:_filter) { :name } it {should equal_to_dom('')} end end context "with non multiple filter" do let(:_grid) do test_report do scope {Entry} filter( :name, :enum, :include_blank => false, :multiple => false, :select => [] ) end end let(:_filter) { :name } it {should equal_to_dom('')} end context "with float filter type" do let(:_grid) { test_report do scope {Entry} filter(:group_id, :float) end } let(:_filter) { :group_id } it { should equal_to_dom( '' )} end context "with enum multiple filter" do let(:_grid) do test_report do scope {Entry} filter(:group_id, :enum, :select => ['hello'], :multiple => true) end end let(:_filter) { :group_id } let(:expected_html) do if ActionPack::VERSION::MAJOR == 3 && ActionPack::VERSION::MINOR < 2 <<-HTML HTML else <<-HTML HTML end end it { should equal_to_dom(expected_html) } end context "with column names filter" do let(:_grid) do test_report(:column_names => [:id, :name]) do scope {Entry} column_names_filter column(:id) column(:name) column(:category) end end let(:_filter) { :column_names } let(:expected_html) do if ActionPack::VERSION::MAJOR == 3 && ActionPack::VERSION::MINOR < 2 <<-HTML HTML else <<-HTML HTML end end it { should equal_to_dom(expected_html) } end context "with column_names_filter default given as symbols" do let(:_grid) do test_report() do scope {Entry} column_names_filter(:default => [:id, :name], :checkboxes => true) column(:id) column(:name) column(:category) end end let(:_filter) { :column_names } let(:expected_html) do <Id DOM end it do should equal_to_dom(expected_html) end end context "with dynamic filter" do let(:filter_options) do {} end let(:_grid) do options = filter_options test_report do scope {Entry} filter(:condition, :dynamic, options) end end let(:_filter) { :condition } context "with no options" do let(:expected_html) do <<-HTML HTML end it {should equal_to_dom(expected_html)} end context "when default option passed" do let(:filter_options) do {:select => [:id, :name], :default => [:id, '>=', 1]} end let(:expected_html) do <<-HTML HTML end it {should equal_to_dom(expected_html)} end context "when select option passed" do let(:filter_options) do {:select => [:id, :name]} end let(:expected_html) do <<-HTML HTML end it {should equal_to_dom(expected_html)} end context "when operation options passed" do let(:filter_options) do {:operations => %w(>= <=), :select => [:id]} end let(:expected_html) do <<-HTML HTML end it {should equal_to_dom(expected_html)} end end end describe ".datagrid_label" do let(:_grid) do test_report do scope {Entry} filter(:name, :string) end end it "should generate label for filter" do expect(view.datagrid_label(:name)).to equal_to_dom( '' ) end it "should pass options through to the helper" do expect(view.datagrid_label(:name, :class => 'foo')).to equal_to_dom( '' ) end it "should support block" do expect(view.datagrid_label(:name, :class => 'foo') { 'The Name' }).to equal_to_dom( '' ) end it "should support explicit label" do expect(view.datagrid_label(:name, "The Name")).to equal_to_dom( '' ) end end end