ekuseru 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ **.swp
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Nugroho Herucahyono
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,99 @@
1
+ = Ekuseru
2
+
3
+ Ekuseru is a gem to generate Microsoft Excel documents with Rails. This gem
4
+ provides templating abilities to create excel documents using Spreadsheet gem.
5
+
6
+ Having a template is very convenient, because we can define how our file looks
7
+ like in the views, and we can access all helper methods defined in rails.
8
+
9
+ == Installation
10
+
11
+ === Spreadsheet gem
12
+
13
+ * Edit your Gemfile, add:
14
+
15
+ gem "ekuseru"
16
+
17
+ * then run
18
+
19
+ bundle install
20
+
21
+ == Usage
22
+
23
+ === Controller
24
+
25
+ To generate xls document, add <code>format.xls</code> in your controller.
26
+
27
+ Example:
28
+
29
+ class ProductsController < ApplicationController
30
+ respond_to :html, :xls
31
+
32
+ def index
33
+ @products = Product.all
34
+ respond_with @products
35
+ end
36
+
37
+ ...
38
+
39
+ end
40
+
41
+ === Template
42
+
43
+ Ekuseru will use .eku files as the template. So, with the example above,
44
+ we will need to create 'index.xls.eku' in app/views/products/.
45
+ Basically it's just an ordinary ruby file.
46
+ In the template, we will get a <code>xls</code> variable which is a
47
+ Spreadsheet::Workbook object ready to be modified like whatever we want.
48
+
49
+ Consult the Spreadsheet documentation to create the template.
50
+
51
+ http://spreadsheet.rubyforge.org/GUIDE_txt.html
52
+
53
+ You can set the filename sent to the user with <code>__filename</code> variable.
54
+
55
+ In the template :
56
+
57
+ # set the filename sent to the user with __filename variable
58
+ # this is optional, if you don't set it, the name will be like products.xls
59
+
60
+ __filename = "Products Catalog.xls"
61
+
62
+ # we get 'xls' variable which is a Workbook object
63
+ # then we can create some worksheet to work with, with create_worksheet method
64
+
65
+ sheet1 = xls.create_worksheet
66
+
67
+ # fill the [0, 0] cell
68
+
69
+ sheet1[0, 0] = "Products Catalog"
70
+
71
+ # Worksheet#row will return a Row object. We can modify it just like an Array.
72
+ # this code will return the second row and fill the cells.
73
+
74
+ sheet1.row(1).concat ["Name", "Price", "Stock", "Description"]
75
+
76
+ # we can access the instance variable we set in the controller, just like
77
+ # in erb template
78
+
79
+ @products.each_with_index do |p, i|
80
+ sheet1.update_row i+2, p.name, p.price, p.stock, p.description
81
+ end
82
+
83
+ # we can add some formatting using Spreadsheet::Format object
84
+
85
+ title_format = Spreadsheet::Format.new(:color => :blue, :weight => :bold, :size => 18)
86
+ sheet1.row(0).set_format(0, title_format)
87
+
88
+ bold = Spreadsheet::Format.new(:weight => :bold)
89
+ sheet1.row(1).default_format = bold
90
+
91
+ That's it. Then you can create a link to the xls file if you want, like:
92
+
93
+ <%= link_to 'Excel', products_path(:format => :xls) %>
94
+
95
+ == Credits
96
+
97
+ * Jacob Rothstein (http://github.com/jbr)
98
+
99
+ Copyright (c) 2009 Nugroho Herucahyono, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the ekuseru plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the ekuseru plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ekuseru'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gemspec|
28
+ gemspec.name = "ekuseru"
29
+ gemspec.summary = "Export excel documents from Rails"
30
+ gemspec.description = "Ekuseru is a gem to generate Microsoft Excel documents with Rails." +
31
+ " This plugin provides templating abilities to create excel documents using Spreadsheet gem."
32
+ gemspec.email = "xinuc@xinuc.org"
33
+ gemspec.homepage = "http://github.com/xinuc/ekuseru"
34
+ gemspec.authors = ["Nugroho Herucahyono", "Jacob Rothstein"]
35
+ gemspec.add_dependency('spreadsheet', ">=0.6")
36
+ end
37
+ rescue LoadError
38
+ puts "Jeweler not available. Install it with: gem install jeweler"
39
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.1
data/ekuseru.gemspec ADDED
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ekuseru}
8
+ s.version = "0.3.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Nugroho Herucahyono", "Jacob Rothstein"]
12
+ s.date = %q{2010-03-22}
13
+ s.description = %q{Ekuseru is a gem to generate Microsoft Excel documents with Rails. This plugin provides templating abilities to create excel documents using Spreadsheet gem.}
14
+ s.email = %q{xinuc@xinuc.org}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "ekuseru.gemspec",
25
+ "init.rb",
26
+ "install.rb",
27
+ "lib/ekuseru.rb",
28
+ "lib/template_handler/eku.rb",
29
+ "lib/template_handler/options.rb",
30
+ "test/ekuseru_controller_test.rb",
31
+ "test/fixtures/ekuseru/index.xls.eku",
32
+ "test/test.log",
33
+ "test/test_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/xinuc/ekuseru}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.6}
39
+ s.summary = %q{Export excel documents from Rails}
40
+ s.test_files = [
41
+ "test/test_helper.rb",
42
+ "test/ekuseru_controller_test.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<spreadsheet>, [">= 0.6"])
51
+ else
52
+ s.add_dependency(%q<spreadsheet>, [">= 0.6"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<spreadsheet>, [">= 0.6"])
56
+ end
57
+ end
58
+
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'ekuseru'
2
+
3
+ Mime::Type.register "application/vnd.ms-excel", :xls
4
+ ActionView::Template.register_template_handler 'eku', Ekuseru::TemplateHandler::Eku
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ puts IO.read(File.join(File.dirname(__FILE__), 'README.rdoc'))
data/lib/ekuseru.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'spreadsheet'
2
+ require 'template_handler/eku'
3
+ require 'template_handler/options'
4
+
5
+ class ActionView::Base
6
+ private
7
+ def _ekuseru_setup
8
+ @_ekuseru_options = Ekuseru::TemplateHandler::Options.new controller
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module Ekuseru
2
+ module TemplateHandler
3
+ class Eku < ActionView::TemplateHandler
4
+ include ActionView::TemplateHandlers::Compilable
5
+
6
+ def compile template
7
+ %{
8
+ _ekuseru_setup
9
+ xls = Spreadsheet::Workbook.new
10
+ #{template.source}
11
+ @_ekuseru_options.set_disposition(__filename ||= nil)
12
+ io = StringIO.new
13
+ xls.write(io)
14
+ io.rewind
15
+ io.read
16
+ }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ module Ekuseru
2
+ module TemplateHandler
3
+ class Options
4
+
5
+ def initialize controller
6
+ @controller = controller
7
+ @controller.response.content_type ||= Mime::XLS
8
+ end
9
+
10
+ def set_disposition(filename = nil)
11
+ @controller.headers["Content-Disposition"] = ["attachment", filename ? "filename=\"#{filename}\"" : nil].compact.join(';')
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class EkuseruController < ActionController::Base
4
+ append_view_path File.join(File.dirname(__FILE__), 'fixtures')
5
+ respond_to :xls
6
+
7
+ def index
8
+ @something = 42
9
+ respond_with @something
10
+ end
11
+ end
12
+
13
+ class EkuseruControllerTest < ActionController::TestCase
14
+ def setup
15
+ get :index, :format => "xls"
16
+ end
17
+
18
+ test "response is success" do
19
+ assert_response :success
20
+ end
21
+
22
+ test "content type is xls" do
23
+ assert_equal Mime::XLS, @response.content_type
24
+ end
25
+
26
+ test "content disposition should be set" do
27
+ assert_equal 'attachment;filename="Products Catalog.xls"', @response.headers['Content-Disposition']
28
+ end
29
+
30
+ test "the response is an xls document" do
31
+ assert_nothing_raised do
32
+ Spreadsheet.open StringIO.new(@response.body)
33
+ end
34
+ end
35
+
36
+ test "the response has the correct data" do
37
+ spreadsheet = Spreadsheet.open StringIO.new(@response.body)
38
+ assert_equal 42, spreadsheet.worksheet(0)[0,0]
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ __filename = "Products Catalog.xls"
2
+
3
+ sheet = xls.create_worksheet
4
+ sheet[0, 0] = @something
data/test/test.log ADDED
@@ -0,0 +1,40 @@
1
+ # Logfile created on Sat Sep 05 23:38:48 -0700 2009 by /
2
+
3
+
4
+ Processing ExcelController#index to xls (for 0.0.0.0 at 2009-09-05 23:45:31) [GET]
5
+ Completed in 130ms (View: 2 | 406 Not Acceptable [http://test.host/excel?format=xls]
6
+
7
+
8
+ Processing ExcelController#index (for 0.0.0.0 at 2009-09-05 23:51:56) [GET]
9
+ Rendering excel/index
10
+ Completed in 147ms (View: 16 | 200 OK [http://test.host/excel]
11
+
12
+
13
+ Processing ExcelController#index (for 0.0.0.0 at 2009-09-05 23:52:04) [GET]
14
+ Rendering excel/index
15
+ Completed in 147ms (View: 17 | 200 OK [http://test.host/excel]
16
+
17
+
18
+ Processing ExcelController#index (for 0.0.0.0 at 2009-09-06 00:03:51) [GET]
19
+ Rendering excel/index
20
+ Completed in 19ms (View: 18 | 200 OK [http://test.host/excel]
21
+
22
+
23
+ Processing ExcelController#index (for 0.0.0.0 at 2009-09-06 00:03:51) [GET]
24
+ Rendering excel/index
25
+ Completed in 53ms (View: 53 | 200 OK [http://test.host/excel]
26
+
27
+
28
+ Processing ExcelController#index (for 0.0.0.0 at 2009-09-06 00:03:51) [GET]
29
+ Rendering excel/index
30
+ Completed in 14ms (View: 13 | 200 OK [http://test.host/excel]
31
+
32
+
33
+ Processing ExcelController#index (for 0.0.0.0 at 2009-09-06 00:03:51) [GET]
34
+ Rendering excel/index
35
+ Completed in 13ms (View: 13 | 200 OK [http://test.host/excel]
36
+
37
+
38
+ Processing ExcelController#index (for 0.0.0.0 at 2009-09-06 00:03:51) [GET]
39
+ Rendering excel/index
40
+ Completed in 18ms (View: 18 | 200 OK [http://test.host/excel]
@@ -0,0 +1,28 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'rubygems'
3
+
4
+ require 'test/unit'
5
+ require 'action_controller'
6
+ require 'action_controller/test_case'
7
+
8
+ require File.join(File.dirname(__FILE__), '..', 'init.rb')
9
+
10
+ ActionController::Routing::Routes.draw do |map|
11
+ match "/index" => "ekuseru#index"
12
+ end
13
+
14
+ class ActiveSupport::TestCase
15
+ end
16
+
17
+ module ActionController
18
+ class TestRequest < ActionDispatch::TestRequest
19
+ def flash
20
+ {}
21
+ end
22
+ end
23
+ end
24
+
25
+ class Hash
26
+ def sweep
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ekuseru
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ - 1
9
+ version: 0.3.1
10
+ platform: ruby
11
+ authors:
12
+ - Nugroho Herucahyono
13
+ - Jacob Rothstein
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-22 00:00:00 +07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: spreadsheet
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 6
31
+ version: "0.6"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Ekuseru is a gem to generate Microsoft Excel documents with Rails. This plugin provides templating abilities to create excel documents using Spreadsheet gem.
35
+ email: xinuc@xinuc.org
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.rdoc
42
+ files:
43
+ - .gitignore
44
+ - MIT-LICENSE
45
+ - README.rdoc
46
+ - Rakefile
47
+ - VERSION
48
+ - ekuseru.gemspec
49
+ - init.rb
50
+ - install.rb
51
+ - lib/ekuseru.rb
52
+ - lib/template_handler/eku.rb
53
+ - lib/template_handler/options.rb
54
+ - test/ekuseru_controller_test.rb
55
+ - test/fixtures/ekuseru/index.xls.eku
56
+ - test/test.log
57
+ - test/test_helper.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/xinuc/ekuseru
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Export excel documents from Rails
88
+ test_files:
89
+ - test/test_helper.rb
90
+ - test/ekuseru_controller_test.rb