Skip to content

Commit

Permalink
Merge branch 'main' into version-2
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Nov 17, 2024
2 parents 5acee62 + 9db10e3 commit 7241336
Show file tree
Hide file tree
Showing 24 changed files with 358 additions and 553 deletions.
4 changes: 2 additions & 2 deletions spec/datagrid/column_names_attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

let(:report) do
options = column_names_filter_options
test_report do
test_grid do
scope { Entry }
column_names_filter(**options)
column(:id)
Expand Down Expand Up @@ -64,7 +64,7 @@

context "when some columns are disabled" do
subject do
test_report do
test_grid do
scope { Entry }
column(:id, mandatory: true)
column(:name)
Expand Down
98 changes: 34 additions & 64 deletions spec/datagrid/columns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
let(:date) { Date.new(2013, 8, 1) }

it "should have data columns without html columns" do
grid = test_report do
grid = test_grid do
scope { Entry }
column(:name)
column(:action, html: true) do
Expand All @@ -38,15 +38,12 @@
end

it "allows a column argument" do
grid = test_report do
scope { Entry }
column(:name)
end
grid = test_grid_column(:name)
expect(grid.data_columns(grid.column_by_name(:name)).map(&:name)).to eq([:name])
end

it "should build rows of data" do
grid = test_report do
grid = test_grid do
scope { Entry }
column(:name)
column(:action, html: true) do
Expand All @@ -56,7 +53,7 @@
expect(grid.rows).to eq([["Star"]])
end
it "should generate header without html columns" do
grid = test_report do
grid = test_grid do
scope { Entry }
column(:name)
column(:action, html: true) do
Expand Down Expand Up @@ -103,7 +100,7 @@ class Report27 < Datagrid::Base
end

it "should return html_columns" do
report = test_report do
report = test_grid do
scope { Entry }
column(:id)
column(:name, html: false)
Expand All @@ -112,7 +109,7 @@ class Report27 < Datagrid::Base
end

it "should return html_columns when column definition has 2 arguments" do
report = test_report(name: "Hello") do
report = test_grid(name: "Hello") do
scope { Entry }
filter(:name)
column(:id)
Expand All @@ -132,10 +129,7 @@ class Report27 < Datagrid::Base
end

it "supports dynamic header" do
grid = test_report do
scope { Entry }
column(:id, header: proc { rand(10**9) })
end
grid = test_grid_column(:id, header: proc { rand(10**9) })

expect(grid.header).to_not eq(grid.header)
end
Expand Down Expand Up @@ -168,7 +162,7 @@ class Report27 < Datagrid::Base
end

it "should support columns with model and report arguments" do
report = test_report(category: "foo") do
report = test_grid(category: "foo") do
scope { Entry.order(:category) }
filter(:category) do |value|
where("category LIKE '%#{value}%'")
Expand Down Expand Up @@ -199,7 +193,7 @@ class Report27 < Datagrid::Base
expect(child.column_by_name(:group_id)).not_to be_nil
end
it "should support defining a query for a column" do
report = test_report do
report = test_grid do
scope { Entry }
filter(:name)
column(:id)
Expand All @@ -210,7 +204,7 @@ class Report27 < Datagrid::Base
end

it "should support post formatting for column defined with query" do
report = test_report do
report = test_grid do
scope { Group.joins(:entries).group("groups.id") }
filter(:name)
column(:entries_count, "count(entries.id)") do |model|
Expand All @@ -223,7 +217,7 @@ class Report27 < Datagrid::Base
expect(report.rows).to eq([["(3)"]])
end
it "should support hidding columns through if and unless" do
report = test_report do
report = test_grid do
scope { Entry }
column(:id, if: :show?)
column(:name, unless: proc { |grid| !grid.show? })
Expand All @@ -240,15 +234,15 @@ def show?

it "raises when incorrect unless option is given" do
expect do
test_report do
test_grid do
column(:id, if: Object.new)
end
end.to raise_error(Datagrid::ConfigurationError)
end

it "raises when :before and :after used together" do
expect do
test_report do
test_grid do
column(:id)
column(:name, before: :id, after: :name)
end
Expand All @@ -257,7 +251,7 @@ def show?

describe ".column_names attributes" do
let(:grid) do
test_report(column_names: %w[id name]) do
test_grid(column_names: %w[id name]) do
scope { Entry }
column(:id)
column(:name)
Expand Down Expand Up @@ -285,12 +279,9 @@ def show?

context "when grid has formatted column" do
it "should output correct data" do
report = test_report do
scope { Entry }
column(:name) do |entry|
format(entry.name) do |value|
"<strong>#{value}</strong"
end
report = test_grid_column(:name) do |entry|
format(entry.name) do |value|
"<strong>#{value}</strong"
end
end
Entry.create!(name: "Hello World")
Expand All @@ -300,7 +291,7 @@ def show?

describe ".default_column_options" do
it "should pass default options to each column definition" do
report = test_report do
report = test_grid do
scope { Entry }
self.default_column_options = { order: false }
column(:id)
Expand All @@ -319,7 +310,7 @@ def show?

describe "fetching data in batches" do
it "should pass the batch size to the find_each method" do
report = test_report do
report = test_grid do
scope { Entry }
column :id
self.batch_size = 25
Expand All @@ -332,7 +323,7 @@ def show?
report.rows
end
it "should be able to disable batches" do
report = test_report do
report = test_grid do
scope { Entry }
column :id
self.batch_size = 0
Expand All @@ -347,7 +338,7 @@ def show?
end

it "should support instance level batch size" do
grid = test_report do
grid = test_grid do
scope { Entry }
column :id
self.batch_size = 25
Expand All @@ -364,7 +355,7 @@ def show?

describe ".data_row" do
it "should give access to column values via an object" do
grid = test_report do
grid = test_grid do
scope { Entry }
column(:id)
column(:name) do
Expand All @@ -389,7 +380,7 @@ def show?
group = Group.create!
Entry.create(group: group)
Entry.create(group: group)
grid = test_report do
grid = test_grid do
scope { Group }
column(:entries_count) do |g|
g.entries.count
Expand All @@ -405,10 +396,7 @@ def show?

describe "instance level column definition" do
let(:modified_grid) do
grid = test_report do
scope { Entry }
column(:id)
end
grid = test_grid_column(:id)
grid.column(:name)
grid
end
Expand Down Expand Up @@ -476,17 +464,11 @@ def show?

describe ".data_value" do
it "should return value" do
grid = test_report do
scope { Entry }
column(:name)
end
grid = test_grid_column(:name)
expect(grid.data_value(:name, Entry.create!(name: "Hello"))).to eq("Hello")
end
it "should raise for disabled columns" do
grid = test_report do
scope { Entry }
column(:name, if: proc { false })
end
grid = test_grid_column(:name, if: proc { false })
expect do
grid.data_value(:name, Entry.create!(name: "Hello"))
end.to raise_error(Datagrid::ColumnUnavailableError)
Expand All @@ -495,7 +477,7 @@ def show?

describe "caching" do
it "should work when enabled in class" do
grid = test_report do
grid = test_grid do
scope { Entry }
self.cached = true
column(:random1) { rand(10**9) }
Expand Down Expand Up @@ -536,7 +518,7 @@ def capitalized_name
end

it "delegates column values to decorator" do
grid = test_report do
grid = test_grid do
scope { Entry }
decorate { |model| EntryDecorator.new(model) }
column(:capitalized_name)
Expand All @@ -552,7 +534,7 @@ def capitalized_name
end

it "allows class decorator" do
grid = test_report do
grid = test_grid do
scope { Entry }
decorate { EntryDecorator }
column(:capitalized_name)
Expand All @@ -563,39 +545,27 @@ def capitalized_name

describe "column scope" do
it "appends preload as non block" do
grid = test_report do
scope { Entry }
column(:id, preload: [:group])
end
grid = test_grid_column(:id, preload: [:group])
expect(grid.assets.preload_values).to_not be_blank
end

it "appends preload with no args" do
grid = test_report do
scope { Entry }
column(:id, preload: -> { order(:id) })
end
grid = test_grid_column(:id, preload: -> { order(:id) })
expect(grid.assets.order_values).to_not be_blank
end

it "appends preload with arg" do
grid = test_report do
scope { Entry }
column(:id, preload: ->(a) { a.order(:id) })
end
grid = test_grid_column(:id, preload: ->(a) { a.order(:id) })
expect(grid.assets.order_values).to_not be_blank
end

it "appends preload as true value" do
grid = test_report do
scope { Entry }
column(:group, preload: true)
end
grid = test_grid_column(:group, preload: true)
expect(grid.assets.preload_values).to eq([:group])
end

it "doesn't append preload when column is invisible" do
grid = test_report do
grid = test_grid do
scope { Entry }
column(:id1, preload: ->(a) { a.order(:id) })
column(:id2, preload: ->(a) { a.order(:id) }, if: ->(_a) { false })
Expand Down
Loading

0 comments on commit 7241336

Please sign in to comment.