Skip to content

Commit

Permalink
ensure that columns defined on the resource class are also present on…
Browse files Browse the repository at this point in the history
… the resource instance.
  • Loading branch information
randym committed Oct 12, 2013
1 parent 3bf5c06 commit 429ab93
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ doc
*.un~
.yardoc
.rbenv-version
.ruby-version
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ gemspec

group :development, :test do
gem 'sqlite3'
gem 'rake', '~> 10.0.4', :require => false
gem 'haml', '~> 3.1.1', :require => false
gem 'yard'
gem 'rdiscount' # For yard
Expand Down
13 changes: 7 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ GEM
arbre (1.0.1)
activesupport (>= 3.0.0)
arel (3.0.2)
axlsx (1.3.1)
axlsx (2.0.1)
htmlentities (~> 4.3.1)
nokogiri (>= 1.4.1)
rubyzip (>= 0.9.5)
rubyzip (~> 1.0.0)
bcrypt-ruby (3.0.1)
bourbon (3.1.3)
sass (>= 3.2.0)
Expand Down Expand Up @@ -149,8 +149,10 @@ GEM
polyamorous (~> 0.5.0)
method_source (0.8.1)
mime-types (1.19)
mini_portile (0.5.1)
multi_json (1.3.7)
nokogiri (1.5.5)
nokogiri (1.6.0)
mini_portile (~> 0.5.0)
orm_adapter (0.4.0)
polyamorous (0.5.0)
activerecord (~> 3.0)
Expand Down Expand Up @@ -183,7 +185,7 @@ GEM
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (10.0.4)
rake (10.1.0)
rdiscount (1.6.8)
rdoc (3.12)
json (~> 1.4)
Expand All @@ -202,7 +204,7 @@ GEM
activesupport (>= 3.0)
railties (>= 3.0)
rspec (~> 2.9.0)
rubyzip (0.9.9)
rubyzip (1.0.0)
sass (3.2.3)
sass-rails (3.2.5)
railties (~> 3.2.0)
Expand Down Expand Up @@ -255,7 +257,6 @@ DEPENDENCIES
jslint_on_rails (~> 1.0.6)
launchy
rails-i18n
rake (~> 10.0.4)
rdiscount
rspec-mocks
rspec-rails (~> 2.9.0)
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,24 @@ bundle exec rake setup
bundle exec rake
```
# Changelog

**2013.10.12**
- Upgraded to most recent version of Axlsx. This introduces some non-backwards compatible
changes and pushes rubyzip up to 1.0.0
- Added support for scoped collections #18

**2013.06.02** Release 2.1.2
- builder#collection is now set on serialize and is available in before and after filters.
- Code cleanup

**2013.04.18** Release 2.1.1
- Fixed issue with repeating data in sheets across downloads
- Updated to use activeadmin 0.6.0+ which supports plugins.

**2013.03.21** Release 2.0.1
- Fixed an issue with missing objects when using the DSL.
Huge thanks to [Fivell](https://github.com/Fivell)

**2012.11.29** Release 2.0.0
- resouce content column are now pre-populated.
- added before and after filters
Expand Down
30 changes: 22 additions & 8 deletions lib/active_admin/axlsx/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,25 @@ def clean_up
end

def export_collection(collection)
header_row
header_row(collection)
collection.each do |resource|
sheet.add_row resource_data(resource)
end
end

# tranform column names into array of localized strings
# @return [Array]
def header_row(collection)
sheet.add_row header_data_for(collection), { :style => header_style_id }
end

def header_data_for(collection)
resource = collection.first
columns.map do |column|
column.localized_name(i18n_scope) if in_scope(resource, column)
end.compact
end

def apply_filter(filter)
filter.call(sheet) if filter
end
Expand All @@ -171,7 +184,14 @@ def parse_options(options)
end

def resource_data(resource)
columns.map { |column| call_method_or_proc_on resource, column.data }
columns.map do |column|
call_method_or_proc_on resource, column.data if in_scope(resource, column)
end
end

def in_scope(resource, column)
return true unless column.name.is_a?(Symbol)
resource.respond_to?(column.name)
end

def sheet
Expand All @@ -182,12 +202,6 @@ def package
@package ||= ::Axlsx::Package.new(:use_shared_strings => true)
end

# tranform column names into array of localized strings
# @return [Array]
def header_row
sheet.add_row columns.map { |column| column.localized_name(i18n_scope) }, :style => header_style_id
end

def header_style_id
package.workbook.styles.add_style header_style
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/axlsx/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ActiveAdmin
module Axlsx
VERSION = '2.1.2'
VERSION = '3.0.0'
end
end

0 comments on commit 429ab93

Please sign in to comment.