Source
/app/controllers/application.rb
class ApplicationController < ActionController::Base
include Authorize
include ExceptionNotifiable
layout "admin"
# Pick a unique cookie name to distinguish our session data from others'
session :session_key => '_4ssoM_session_id'
#password will be hashed before saving in log file
filter_parameter_logging :password
protected
helper_method :logged_in?, :current_user
helper_method :permit?, :admin_restrict_to
def localize
ActiveScaffold::Localization.lang = session[:lang_code] unless session[:lang_code].nil?
end
end
/app/controllers/companies_controller.rb
class CompaniesController < ApplicationController
append_before_filter :localize
layout "demo"
ActiveScaffold.set_defaults do |config|
config.ignore_columns.add [:created_at, :updated_at]
config.left_handed = true
end
active_scaffold :company do |config|
config.label = "Customers"
config.actions.swap :search, :field_search
config.actions << :customize
config.actions << :print_html
config.actions << :print_pdf
config.actions << :export_tool
config.print_pdf.header_text = "Customer List"
# Any Column#method can be used as a key to the columns_by_key_value hash, for example:
# [:phone => {
# :description = "(Format: ###-###-####)"
# }],
# We have defined a few synonyms so that you can use the language you prefer:
# columns_by_key_value or has_columns
# :exclude or :except
# :form_ui or :type
# Because we are using ActiveScaffoldTools - which comes with date, file and boolean bridges -
# we do not have to set the type or form_ui for :active_at, :active or :signup_on but we do
# for demo purposes.
columns_by_key_value [
[:name => {
:inplace_edit => true,
:options => {:print_width => "240"},
:required => true
}],
[:phone => {
:description => "(Format: ###-###-####)",
:options => {:print_width => "140"},
:type => :usa_phone,
:required => true
}],
[:state => {
:exclude => [:list],
:options => {:print_width => "80"},
:type => :usa_state,
:required => true
}],
[:active => {
:form_ui => :checkbox,
:inplace_edit => true,
:options => {:print_width => "80"}
}],
[:signup_on => {
# Force time formatting by uncommenting this line
# :options => {:showsTime => 1},
:except => [],
:options => {:field_search => :range},
:type => :calendar
}],
[:comment => {
:except => [:list],
:options => {:print_width => "440", :rows => 10, :cols => 40},
:type => :textarea
}],
[:active_at => {
:except => [:field_search]
}]
]
end
def current_user
true
end
def delete_all
Company.destroy_all
index
end
def change_language
session[:lang_code] = params[:lang_code]
redirect_to :action => :list
end
end
/app/helpers/companies_helper.rb
module CompaniesHelper
end
/app/models/company.rb
# == Schema Information
# Schema version: 11
#
# Table name: companies
#
# id :integer(11) not null, primary key
# name :string(255)
# phone :string(255)
# created_at :datetime
# updated_at :datetime
# company_type_id :integer(11)
# signup_on :date
# active :boolean(1)
# state :string(255)
# comment :string(255)
# active_at :datetime
#
class Company < ActiveRecord::Base
validates_length_of :name, :within => 3..20
validates_presence_of :name, :state
validates_format_of :phone, :with => /^\d\d\d-\d\d\d-\d\d\d\d$/
# ===================
# = Authorize BEGIN =
# ===================
def authorized_for_create?
true
end
def authorized_for_read?
true
end
def authorized_for_update?
true
end
def authorized_for_delete?
true
end
# =================
# = Authorize END =
# =================
end
/app/views/layouts/demo.rhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<%= javascript_include_tag :defaults, "code_highlighter/code_highlighter", "code_highlighter/ruby" %>
<%= dhtml_calendar_includes("blue", session[:lang_code]) %>
<%= active_scaffold_includes %>
<%= javascript_include_tag "code_highlighter" %>
<%= javascript_include_tag "form_enhancements" %>
<%= javascript_include_tag "ruby" %>
<%= stylesheet_link_tag "reset", "main", "demo", :media => 'all' %>
<!--[if IE]><%= stylesheet_link_tag "main-ie", :media => "all" %><![endif]-->
<meta name="Generator" content="iWeb 1.1.1" />
<title>DRY Scaffold</title>
</head>
<body>
<div id="wrap">
<div id="header"><a href="/"><h1>4 Rails</h1></a>
<h2 class="description">Rails for the Business</h2>
<ul id="nav">
<% Section.find(:all).each do |section| %>
<li><%= link_to section.name, {:controller => :as_docs, :action => :index, :section => section.id} %></li>
<% end %>
<li><%= link_to "Demo", {:controller => :companies, :action => :index}, {:class => "selected"} %></li>
<div class="clear-fix"></div>
</ul>
<p id="latest-release">
<small>
<!-- <a href="http://activescaffold.com">Help</a> -->
</small>
</p>
</div>
<table id="purge-data">
<tr>
<!-- <td>
<%= as_("Feel free to wipe out the data.") %>
</td>
<td>
<%= form_tag :action => :delete_all %>
<input name="commit" type="submit" value="Purge Data" />
</form>
</td> -->
<td>
<%= as_("Change the Language") %>
</td>
<td>
<% form_tag(:action => "change_language") do -%>
<%= select_tag(nil, options_for_select(["- select -"] + ACTIVE_SCAFFOLD_LANG_OPTIONS, session[:lang_code]), {:name => :lang_code, :onchange => "javascript:submit();"}) %>
<%- end -%>
</td>
</tr>
</table>
<div style="margin: 0px 30px 30px 30px">
<% if flash[:notice] -%>
<div id="notice"><%= flash[:notice] %></div>
<% end -%>
<%= yield %>
<div id="content">
<h3>Source</h3>
<%= show_code("app/controllers", "application.rb") -%>
<%= show_code("app/controllers", "companies_controller.rb") -%>
<%= show_code("app/helpers", "companies_helper.rb") -%>
<%= show_code("app/models", "company.rb") -%>
<%= show_code("app/views/layouts", "demo.rhtml") -%>
<%= show_code("config", "routes.rb") -%>
<%= show_code("app/controllers", "report_controller.rb") -%>
<%= show_code("app/helpers", "report_helper.rb") -%>
<%= show_code("app/views/report", "sample.rfpdf") -%>
</div>
</div>
</div>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2542455-1";
urchinTracker();
</script>
</body>
</html>
/config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.connect '', :controller => "as_docs"
map.connect ':controller/service.wsdl', :action => 'wsdl'
map.connect ':controller/:action'
map.connect ':controller/:action/:id'
map.admin '/admin', :controller => "admin/sections"
end
/app/controllers/report_controller.rb
class ReportController < ApplicationController
layout nil
# MAKE SURE THESE METHODS ARE EXCLUDED FROM ANY LAYOUTS! THE PDF MUST BE PURE - NO HTML
def sample
@company_name = "Apple"
@options_for_rfpdf ||= {}
@options_for_rfpdf[:file_name] = "sample.pdf"
end
def sample_source
render :action => "sample_source", :layout => "source"
end
def sample_unicode
render :action => "sample_unicode", :layout => "source"
end
def test_unicode
render :file => "#{File.dirname(__FILE__)}/../../vendor/plugins/rfpdf/test_unicode.rfpdf"
end
def test_arabic
render :file => "#{File.dirname(__FILE__)}/../../vendor/plugins/rfpdf/test_arabic.rfpdf"
end
protected
end
/app/helpers/report_helper.rb
module ReportHelper
include RFPDF
RCOLOR_PALETTE = {
:black => [0x00, 0x00, 0x00],
:dark_blue => [0x3E, 0x78, 0x98],
:dark_yellow => [0xB2,0x43,0x00],
:medium_blue => [0x78,0x9A,0xA7],
:medium_yellow => [0xD8,0x8D,0x5A],
:light_yellow => [0xF4,0xD1,0x8A],
:light_blue => [0xD8,0xE5,0xEC],
:light_red => [0xEB,0xAE,0xAF],
:light_green => [0xA8,0xE2,0xB8],
:white => [0xff, 0xff, 0xff],
}.freeze
end
/app/views/report/sample.rfpdf
<%
pdf = TCPDF.new()
left_margin = 15
right_margin = 15
pdf.SetMargins(left_margin, 27, right_margin);
pdf.AddPage();
#pdf.SetFont("FreeSans", "", 12);
# pdf.is_unicode = false
text = "Company Name: "
text << @company_name || "Company's Name"
right_margin_from_right_edge = 40
#
# English
#
x = left_margin
y = 10
text_options = {:font => "freesans"}
pdf.draw_title(x, y, "Company Information", text_options)
y += 20
pdf.draw_text(x, y, text, text_options.merge(:font_size => 12))
x1 = left_margin
x2 = 100
y1 = y2 = y + 20
pdf.draw_line(x1, y1, x2, y2, :line_color => ReportHelper::RCOLOR_PALETTE[:dark_blue], :line_width => 1)
y += 20
text_block = "Here is a block of text about the company. Here is a block of text about the company. Here is a block of text about the company. Here is a block of text about the company. Here is a block of text about the company. Here is a block of text about the company. Here is a block of text about the company."
pdf.draw_text_block(x, y, text_block, left_margin + 20, right_margin_from_right_edge, text_options.merge(:font_size => 12, :font_style => 'I'))
y = pdf.GetY() + 20
pdf.draw_box(x, y, 30, 20, :border => 1,
:border_color => ReportHelper::RCOLOR_PALETTE[:dark_yellow],
:border_width => 1.5,
:fill => 1,
:fill_color => ReportHelper::RCOLOR_PALETTE[:light_yellow])
y += 30
pdf.draw_text_box(x, y, 40, 15, "Text In a Box", text_options.merge(:border => 1,
:border_color => ReportHelper::RCOLOR_PALETTE[:dark_blue],
:border_width => 0.5,
:fill => 1,
:fill_color => ReportHelper::RCOLOR_PALETTE[:light_blue],
:font_size => 12))
pdf.SetLeftMargin left_margin
pdf.SetRightMargin right_margin_from_right_edge
pdf.SetXY(left_margin, y + 20)
pdf.SetFont(text_options[:font], '', 10)
html = "<b>How To Get Simple HTML</b><br/>Into PDF is not so <i>difficult</i> after all."
pdf.writeHTML(html)#, :height => 10)
%><%= pdf.Output() %>