diff --git a/lib/active_admin/axlsx/builder.rb b/lib/active_admin/axlsx/builder.rb index 6fb28ad..68393a4 100644 --- a/lib/active_admin/axlsx/builder.rb +++ b/lib/active_admin/axlsx/builder.rb @@ -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 @@ -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 diff --git a/lib/active_admin/axlsx/resource_controller_extension.rb b/lib/active_admin/axlsx/resource_controller_extension.rb index 5e002e4..9be8f5c 100644 --- a/lib/active_admin/axlsx/resource_controller_extension.rb +++ b/lib/active_admin/axlsx/resource_controller_extension.rb @@ -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 diff --git a/spec/axlsx/unit/builder_spec.rb b/spec/axlsx/unit/builder_spec.rb index 8ba5831..06ce304 100644 --- a/spec/axlsx/unit/builder_spec.rb +++ b/spec/axlsx/unit/builder_spec.rb @@ -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 @@ -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 @@ -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 @@ -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'] @@ -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 @@ -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