Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to execute controller's view_context methods in column builder block #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/active_admin/axlsx/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ def delete_columns(*column_names)

# Serializes the collection provided
# @return [Axlsx::Package]
def serialize(collection)
def serialize(collection, view_context)
@collection = collection
@view_context = view_context
apply_filter @before_filter
export_collection(collection)
apply_filter @after_filter
Expand Down Expand Up @@ -223,6 +224,14 @@ def resource_columns(resource)
Column.new(column.name.to_sym)
end
end

def method_missing(method_name, *arguments)
if @view_context.respond_to? method_name
@view_context.send method_name, *arguments
else
super
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/active_admin/axlsx/resource_controller_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.included(base)
def index_with_xlsx(options={}, &block)
index_without_xlsx(options) do |format|
format.xlsx do
xlsx = active_admin_config.xlsx_builder.serialize(collection)
xlsx = active_admin_config.xlsx_builder.serialize(collection, view_context)
send_data xlsx, :filename => "#{xlsx_filename}", :type => Mime::Type.lookup_by_extension(:xlsx)
end
end
Expand Down
41 changes: 36 additions & 5 deletions spec/axlsx/unit/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ module Axlsx
builder.columns.last.data.call(post).should == "Hot Dawg - with cheese"
end
end

context 'Using helper methods in column generation' do
let(:post) { Post.new(:title => "Hot Dawg") }
let(:view_context) do
obj = {}
def obj.helper_method(title)
"#{title} from helper_method in view_context"
end
obj
end

before do
builder.instance_variable_set :@view_context, view_context
builder.column(:hoge) { |resource| helper_method(resource.title) }
end

it 'stores the block when defining a column for later execution.' do
builder.instance_exec(post, &builder.columns.last.data).should == "Hot Dawg from helper_method in view_context"
end
end
end

context 'sheet generation without headers' do
Expand All @@ -69,7 +89,7 @@ module Axlsx
Post.stub!(:all) { posts }
# disable clean up so we can get the package.
builder.stub(:clean_up) { false }
builder.serialize(Post.all)
builder.serialize(Post.all, nil)
@package = builder.send(:package)
@collection = builder.collection
end
Expand Down Expand Up @@ -98,7 +118,7 @@ module Axlsx
Post.stub!(:all) { posts }
# disable clean up so we can get the package.
builder.stub(:clean_up) { false }
builder.serialize(Post.all)
builder.serialize(Post.all, nil)
@package = builder.send(:package)
@collection = builder.collection
end
Expand All @@ -111,15 +131,22 @@ module Axlsx
end

context 'Sheet generation with a highly customized configuration.' do

let!(:users) { [User.new(first_name: 'bob', last_name: 'nancy')] }

let!(:posts) { [Post.new(title: 'bob', body: 'is a swell guy', author: users.first)] }

let(:view_context) do
obj = {}
def obj.augment(text)
"augmented #{text}"
end
obj
end

let!(:builder) {
Builder.new(Post, header_style: { sz: 10, fg_color: "FF0000" }, i18n_scope: [:axlsx, :post]) do
delete_columns :id, :created_at, :updated_at
column(:author) { |resource| "#{resource.author.first_name} #{resource.author.last_name}" }
column(:author) { |resource| augment "#{resource.author.first_name} #{resource.author.last_name}" }
after_filter { |sheet|
sheet.add_row []
sheet.add_row ['Author Name', 'Number of Posts']
Expand Down Expand Up @@ -150,7 +177,7 @@ module Axlsx
Post.stub!(:all) { posts }
# disable clean up so we can get the package.
builder.stub(:clean_up) { false }
builder.serialize(Post.all)
builder.serialize(Post.all, view_context)
@package = builder.send(:package)
@collection = builder.collection
end
Expand Down Expand Up @@ -189,6 +216,10 @@ module Axlsx
it 'has no OOXML validation errors' do
@package.validate.size.should == 0
end

it 'executes helper method from view_context passed to serialize method' do
builder.instance_exec(posts.first, &builder.columns.last.data).should == 'augmented Set In Proc nancy'
end
end
end
end
Expand Down