amrita2 1.9.6 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +112 -0
- data/init.rb +6 -0
- data/lib/amrita2/gettext.rb +116 -0
- data/lib/amrita2/macro.rb +153 -0
- data/lib/amrita2/rails_bridge.rb +172 -26
- data/lib/amrita2/template.rb +2634 -234
- data/lib/amrita2/testsupport.rb +171 -0
- data/lib/amrita2/version.rb +3 -3
- data/lib/amrita2.rb +1 -0
- data/sample/depot/app/controllers/admin_controller.rb +59 -0
- data/sample/depot/app/controllers/application.rb +20 -0
- data/sample/depot/app/controllers/info_controller.rb +19 -0
- data/sample/depot/app/controllers/login_controller.rb +85 -0
- data/sample/depot/app/controllers/store_controller.rb +68 -0
- data/sample/depot/app/helpers/admin_helper.rb +7 -0
- data/sample/depot/app/helpers/application_helper.rb +10 -0
- data/sample/depot/app/helpers/ar_form.rb +169 -0
- data/sample/depot/app/helpers/form_tag.rb +24 -0
- data/sample/depot/app/helpers/info_helper.rb +7 -0
- data/sample/depot/app/helpers/standard_form.rb +73 -0
- data/sample/depot/app/helpers/store_helper.rb +14 -0
- data/sample/depot/app/models/cart.rb +36 -0
- data/sample/depot/app/models/cart_item.rb +26 -0
- data/sample/depot/app/models/line_item.rb +34 -0
- data/sample/depot/app/models/order.rb +57 -0
- data/sample/depot/app/models/product.rb +41 -0
- data/sample/depot/app/models/user.rb +83 -0
- data/sample/depot/config/boot.rb +49 -0
- data/sample/depot/config/environment.rb +83 -0
- data/sample/depot/config/environments/development.rb +24 -0
- data/sample/depot/config/environments/production.rb +24 -0
- data/sample/depot/config/environments/test.rb +24 -0
- data/sample/depot/config/routes.rb +10 -0
- data/sample/depot/db/migrate/001_create_products.rb +18 -0
- data/sample/depot/db/migrate/002_add_price.rb +14 -0
- data/sample/depot/db/migrate/003_add_test_data.rb +68 -0
- data/sample/depot/db/migrate/004_add_sessions.rb +20 -0
- data/sample/depot/db/migrate/005_create_orders.rb +21 -0
- data/sample/depot/db/migrate/006_create_line_items.rb +27 -0
- data/sample/depot/db/migrate/007_create_users.rb +18 -0
- data/sample/depot/db/schema.rb +45 -0
- data/sample/depot/public/dispatch.rb +15 -0
- data/sample/depot/test/functional/admin_controller_test.rb +54 -0
- data/sample/depot/test/functional/info_controller_test.rb +23 -0
- data/sample/depot/test/functional/login_controller_test.rb +74 -0
- data/sample/depot/test/functional/store_controller_test.rb +57 -0
- data/sample/depot/test/integration/dsl_user_stories_test.rb +126 -0
- data/sample/depot/test/integration/user_stories_test.rb +70 -0
- data/sample/depot/test/performance/order_speed_test.rb +58 -0
- data/sample/depot/test/test_helper.rb +16 -0
- data/sample/depot/test/unit/cart_test.rb +39 -0
- data/sample/depot/test/unit/cart_test1.rb +31 -0
- data/sample/depot/test/unit/line_item_test.rb +15 -0
- data/sample/depot/test/unit/order_test.rb +15 -0
- data/sample/depot/test/unit/product_test.rb +98 -0
- data/sample/depot/vendor/plugins/amrita2/init.rb +6 -0
- data/sample/hello/hello.rb +22 -0
- data/sample/login_engine/app/controllers/application.rb +16 -0
- data/sample/login_engine/app/controllers/user_controller.rb +265 -0
- data/sample/login_engine/app/helpers/application_helper.rb +3 -0
- data/sample/login_engine/app/helpers/form_tag.rb +16 -0
- data/sample/login_engine/app/helpers/two_columns.rb +24 -0
- data/sample/login_engine/app/helpers/two_columns_form.rb +47 -0
- data/sample/login_engine/app/helpers/user_helper.rb +88 -0
- data/sample/login_engine/app/models/user.rb +7 -0
- data/sample/login_engine/app/models/user_notify.rb +75 -0
- data/sample/login_engine/config/boot.rb +45 -0
- data/sample/login_engine/config/environment.rb +140 -0
- data/sample/login_engine/config/environments/development.rb +21 -0
- data/sample/login_engine/config/environments/production.rb +18 -0
- data/sample/login_engine/config/environments/test.rb +19 -0
- data/sample/login_engine/config/routes.rb +23 -0
- data/sample/login_engine/db/migrate/001_create_users.rb +25 -0
- data/sample/login_engine/db/schema.rb +25 -0
- data/sample/login_engine/lib/config.rb +113 -0
- data/sample/login_engine/lib/hpricot_test_extension.rb +80 -0
- data/sample/login_engine/lib/login_engine/authenticated_system.rb +113 -0
- data/sample/login_engine/lib/login_engine/authenticated_user.rb +155 -0
- data/sample/login_engine/lib/login_engine.rb +62 -0
- data/sample/login_engine/public/dispatch.rb +10 -0
- data/sample/login_engine/test/functional/amrita2_test.rb +267 -0
- data/sample/login_engine/test/functional/user_controller_test.rb +544 -0
- data/sample/login_engine/test/mocks/mail.rb +14 -0
- data/sample/login_engine/test/mocks/time.rb +19 -0
- data/sample/login_engine/test/test_helper.rb +31 -0
- data/sample/login_engine/test/unit/user_test.rb +116 -0
- data/sample/login_engine/vendor/plugins/amrita2/init.rb +6 -0
- data/specs/attribute.rb +201 -0
- data/specs/datatypes.rb +231 -0
- data/specs/dictionary.rb +68 -0
- data/specs/erb_cdata.rb +187 -0
- data/specs/filters.rb +513 -0
- data/specs/gettext/erb_gettext.rb +42 -0
- data/specs/gettext/gettext_util.rb +65 -0
- data/specs/gettext/static_text.rb +103 -0
- data/specs/impl/code_generator.rb +87 -0
- data/specs/impl/dynamic_element.rb +92 -0
- data/specs/impl/hash_delegator.rb +57 -0
- data/specs/impl/parse_opt.rb +34 -0
- data/specs/impl/preprocess.rb +823 -0
- data/specs/impl/testsupport.rb +89 -0
- data/specs/inlineruby.rb +429 -0
- data/specs/intro.rb +654 -0
- data/specs/loop.rb +203 -0
- data/specs/macro.rb +532 -0
- data/specs/sample.rb +34 -0
- data/specs/sanitize.rb +110 -0
- data/specs/template.rb +189 -0
- data/specs/trace.rb +97 -0
- metadata +138 -19
- data/lib/amrita2/core.rb +0 -1897
- data/lib/amrita2/rd.rb +0 -314
@@ -0,0 +1,73 @@
|
|
1
|
+
|
2
|
+
class StandardForm < Amrita2::Macro::Base
|
3
|
+
TemplateText = <<-END_OF_TEMPLATE
|
4
|
+
<<fieldset<
|
5
|
+
<<legend:title>>
|
6
|
+
<%%= form_tag(<%= $_[:url].inspect %>) %%>
|
7
|
+
<<:header>>
|
8
|
+
<<p:items<
|
9
|
+
<<label:label|Attr[:for]<
|
10
|
+
<<:text>>:
|
11
|
+
<<:input>>
|
12
|
+
<<:fotter>>
|
13
|
+
<%%= '</form>' %%>
|
14
|
+
|
15
|
+
END_OF_TEMPLATE
|
16
|
+
|
17
|
+
def macro_data(element)
|
18
|
+
root = element.as_amrita_dictionary
|
19
|
+
record = root[:record].intern
|
20
|
+
title = element.search("form_title").first
|
21
|
+
url = element.search("url").first
|
22
|
+
header = element.search("header").first
|
23
|
+
items = element.search("tr").collect do |tr|
|
24
|
+
tr_to_data(record, tr)
|
25
|
+
end
|
26
|
+
fotter = element.search("fotter").first
|
27
|
+
|
28
|
+
ret = {
|
29
|
+
:title => title.contents,
|
30
|
+
:record => record,
|
31
|
+
:url => url ? url.as_amrita_dictionary : {},
|
32
|
+
:header => header.contents,
|
33
|
+
:items => items,
|
34
|
+
:fotter => fotter.contents
|
35
|
+
}
|
36
|
+
ret
|
37
|
+
end
|
38
|
+
|
39
|
+
def tr_to_data(record, tr)
|
40
|
+
label = tr.search("th").first
|
41
|
+
td = tr.search("td").first
|
42
|
+
input = td_to_input(record, td) || { }
|
43
|
+
{
|
44
|
+
:label => {
|
45
|
+
:for => "#{record}_#{input[:field_id]}",
|
46
|
+
:text => label.contents.strip,
|
47
|
+
},
|
48
|
+
:input => input[:input]
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
def td_to_input(record, td)
|
53
|
+
x = td.children.find { |c| c.kind_of?(Hpricot::Elem) }
|
54
|
+
raise "input was not found in <td>" unless x
|
55
|
+
attr = x.as_amrita_dictionary
|
56
|
+
field_id = attr.delete(:id)
|
57
|
+
input = case x.name
|
58
|
+
when "text"
|
59
|
+
"<%= text_field #{record.to_s.inspect}, #{field_id.to_s.inspect}, #{attr.inspect} %>"
|
60
|
+
when "text_area"
|
61
|
+
"<%= text_area #{record.to_s.inspect}, #{field_id.to_s.inspect}, #{attr.inspect} %>"
|
62
|
+
when "field"
|
63
|
+
x.contents
|
64
|
+
else
|
65
|
+
raise "unknown input field #{x.name} "
|
66
|
+
end
|
67
|
+
{
|
68
|
+
:input => Amrita2::SanitizedString[input],
|
69
|
+
:field_id => field_id
|
70
|
+
}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
module StoreHelper
|
7
|
+
|
8
|
+
def hidden_div_if(condition, attributes = {}, &block)
|
9
|
+
if condition
|
10
|
+
attributes["style"] = "display: none"
|
11
|
+
end
|
12
|
+
content_tag("div", attributes, &block)
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
class Cart
|
7
|
+
include Amrita2::DictionaryData
|
8
|
+
|
9
|
+
attr_reader :items
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@items = []
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def add_product(product)
|
17
|
+
current_item = @items.find {|item| item.product == product}
|
18
|
+
if current_item
|
19
|
+
current_item.increment_quantity
|
20
|
+
else
|
21
|
+
current_item = CartItem.new(product)
|
22
|
+
@items << current_item
|
23
|
+
end
|
24
|
+
current_item
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def total_items
|
29
|
+
@items.sum { |item| item.quantity }
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def total_price
|
34
|
+
@items.sum { |item| item.price }
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
class CartItem
|
7
|
+
|
8
|
+
attr_reader :product, :quantity
|
9
|
+
|
10
|
+
def initialize(product)
|
11
|
+
@product = product
|
12
|
+
@quantity = 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def increment_quantity
|
16
|
+
@quantity += 1
|
17
|
+
end
|
18
|
+
|
19
|
+
def title
|
20
|
+
@product.title
|
21
|
+
end
|
22
|
+
|
23
|
+
def price
|
24
|
+
@product.price * @quantity
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
# Schema as of June 12, 2006 15:45 (schema version 7)
|
7
|
+
#
|
8
|
+
# Table name: line_items
|
9
|
+
#
|
10
|
+
# id :integer(11) not null, primary key
|
11
|
+
# product_id :integer(11) default(0), not null
|
12
|
+
# order_id :integer(11) default(0), not null
|
13
|
+
# quantity :integer(11) default(0), not null
|
14
|
+
# total_price :integer(11) default(0), not null
|
15
|
+
#
|
16
|
+
|
17
|
+
|
18
|
+
class LineItem < ActiveRecord::Base
|
19
|
+
|
20
|
+
belongs_to :order
|
21
|
+
belongs_to :product
|
22
|
+
|
23
|
+
|
24
|
+
def self.from_cart_item(cart_item)
|
25
|
+
li = self.new
|
26
|
+
li.product = cart_item.product
|
27
|
+
li.quantity = cart_item.quantity
|
28
|
+
li.total_price = cart_item.price
|
29
|
+
li
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
# Schema as of June 12, 2006 15:45 (schema version 7)
|
7
|
+
#
|
8
|
+
# Table name: orders
|
9
|
+
#
|
10
|
+
# id :integer(11) not null, primary key
|
11
|
+
# name :string(255)
|
12
|
+
# address :text
|
13
|
+
# email :string(255)
|
14
|
+
# pay_type :string(10)
|
15
|
+
#
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
class Order < ActiveRecord::Base
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
has_many :line_items
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
PAYMENT_TYPES = [
|
29
|
+
# Displayed stored in db
|
30
|
+
[ "Check", "check" ],
|
31
|
+
[ "Credit card", "cc" ],
|
32
|
+
[ "Purchase order", "po" ]
|
33
|
+
]
|
34
|
+
|
35
|
+
# ...
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
validates_presence_of :name, :address, :email, :pay_type
|
40
|
+
validates_inclusion_of :pay_type, :in => PAYMENT_TYPES.map {|disp, value| value}
|
41
|
+
|
42
|
+
# ...
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
def add_line_items_from_cart(cart)
|
47
|
+
cart.items.each do |item|
|
48
|
+
li = LineItem.from_cart_item(item)
|
49
|
+
line_items << li
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
end
|
57
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
# Schema as of June 12, 2006 15:45 (schema version 7)
|
7
|
+
#
|
8
|
+
# Table name: products
|
9
|
+
#
|
10
|
+
# id :integer(11) not null, primary key
|
11
|
+
# title :string(255)
|
12
|
+
# description :text
|
13
|
+
# image_url :string(255)
|
14
|
+
# price :integer(11) default(0)
|
15
|
+
#
|
16
|
+
|
17
|
+
|
18
|
+
class Product < ActiveRecord::Base
|
19
|
+
|
20
|
+
has_many :orders, :through => :line_items
|
21
|
+
|
22
|
+
has_many :line_items
|
23
|
+
|
24
|
+
def self.find_products_for_sale
|
25
|
+
find(:all, :order => "title")
|
26
|
+
end
|
27
|
+
|
28
|
+
validates_presence_of :title, :description, :image_url
|
29
|
+
validates_numericality_of :price
|
30
|
+
validates_uniqueness_of :title
|
31
|
+
validates_format_of :image_url,
|
32
|
+
:with => %r{\.(gif|jpg|png)$}i,
|
33
|
+
:message => "must be a URL for a GIF, JPG, or PNG image"
|
34
|
+
protected
|
35
|
+
|
36
|
+
def validate
|
37
|
+
errors.add(:price, "should be at least 0.01") if price.nil? || price < 0.01
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
# Schema as of June 12, 2006 15:45 (schema version 7)
|
7
|
+
#
|
8
|
+
# Table name: users
|
9
|
+
#
|
10
|
+
# id :integer(11) not null, primary key
|
11
|
+
# name :string(255)
|
12
|
+
# hashed_password :string(255)
|
13
|
+
# salt :string(255)
|
14
|
+
#
|
15
|
+
|
16
|
+
require 'digest/sha1'
|
17
|
+
|
18
|
+
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
|
21
|
+
validates_presence_of :name
|
22
|
+
validates_uniqueness_of :name
|
23
|
+
|
24
|
+
attr_accessor :password_confirmation
|
25
|
+
validates_confirmation_of :password
|
26
|
+
|
27
|
+
def validate
|
28
|
+
errors.add_to_base("Missing password") if hashed_password.blank?
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
def self.authenticate(name, password)
|
34
|
+
user = self.find_by_name(name)
|
35
|
+
if user
|
36
|
+
expected_password = encrypted_password(password, user.salt)
|
37
|
+
if user.hashed_password != expected_password
|
38
|
+
user = nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
user
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
# 'password' is a virtual attribute
|
46
|
+
|
47
|
+
def password
|
48
|
+
@password
|
49
|
+
end
|
50
|
+
|
51
|
+
def password=(pwd)
|
52
|
+
@password = pwd
|
53
|
+
return if pwd.blank?
|
54
|
+
create_new_salt
|
55
|
+
self.hashed_password = User.encrypted_password(self.password, self.salt)
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
def after_destroy
|
61
|
+
if User.count.zero?
|
62
|
+
raise "Can't delete last user"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
|
70
|
+
def create_new_salt
|
71
|
+
self.salt = self.object_id.to_s + rand.to_s
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
def self.encrypted_password(password, salt)
|
77
|
+
string_to_hash = password + "wibble" + salt
|
78
|
+
Digest::SHA1.hexdigest(string_to_hash)
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
end
|
83
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
# Don't change this file. Configuration is done in config/environment.rb and config/environments/*.rb
|
7
|
+
|
8
|
+
unless defined?(RAILS_ROOT)
|
9
|
+
root_path = File.join(File.dirname(__FILE__), '..')
|
10
|
+
|
11
|
+
unless RUBY_PLATFORM =~ /mswin32/
|
12
|
+
require 'pathname'
|
13
|
+
root_path = Pathname.new(root_path).cleanpath(true).to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
RAILS_ROOT = root_path
|
17
|
+
end
|
18
|
+
|
19
|
+
unless defined?(Rails::Initializer)
|
20
|
+
if File.directory?("#{RAILS_ROOT}/vendor/rails")
|
21
|
+
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
|
22
|
+
else
|
23
|
+
require 'rubygems'
|
24
|
+
|
25
|
+
environment_without_comments = IO.readlines(File.dirname(__FILE__) + '/environment.rb').reject { |l| l =~ /^#/ }.join
|
26
|
+
environment_without_comments =~ /[^#]RAILS_GEM_VERSION = '([\d.]+)'/
|
27
|
+
rails_gem_version = $1
|
28
|
+
|
29
|
+
if version = defined?(RAILS_GEM_VERSION) ? RAILS_GEM_VERSION : rails_gem_version
|
30
|
+
rails_gem = Gem.cache.search('rails', "=#{version}").first
|
31
|
+
|
32
|
+
if rails_gem
|
33
|
+
require_gem "rails", "=#{version}"
|
34
|
+
require rails_gem.full_gem_path + '/lib/initializer'
|
35
|
+
else
|
36
|
+
STDERR.puts %(Cannot find gem for Rails =#{version}:
|
37
|
+
Install the missing gem with 'gem install -v=#{version} rails', or
|
38
|
+
change environment.rb to define RAILS_GEM_VERSION with your desired version.
|
39
|
+
)
|
40
|
+
exit 1
|
41
|
+
end
|
42
|
+
else
|
43
|
+
gem "rails"
|
44
|
+
require 'initializer'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
Rails::Initializer.run(:set_load_path)
|
49
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
# Be sure to restart your web server when you modify this file.
|
7
|
+
|
8
|
+
# Uncomment below to force Rails into production mode when
|
9
|
+
# you don't control web/app server and can't set it the proper way
|
10
|
+
# ENV['RAILS_ENV'] ||= 'production'
|
11
|
+
|
12
|
+
# Bootstrap the Rails environment, frameworks, and default configuration
|
13
|
+
require File.join(File.dirname(__FILE__), 'boot')
|
14
|
+
|
15
|
+
Rails::Initializer.run do |config|
|
16
|
+
# Settings in config/environments/* take precedence those specified here
|
17
|
+
|
18
|
+
# Skip frameworks you're not going to use
|
19
|
+
# config.frameworks -= [ :action_web_service, :action_mailer ]
|
20
|
+
|
21
|
+
# Add additional load paths for your own custom dirs
|
22
|
+
# config.load_paths += %W( #{RAILS_ROOT}/extras )
|
23
|
+
|
24
|
+
# Force all environments to use the same logger level
|
25
|
+
# (by default production uses :info, the others :debug)
|
26
|
+
# config.log_level = :debug
|
27
|
+
|
28
|
+
# Use the database for sessions instead of the file system
|
29
|
+
# (create the session table with 'rake create_sessions_table')
|
30
|
+
config.action_controller.session_store = :active_record_store
|
31
|
+
|
32
|
+
# Use SQL instead of Active Record's schema dumper when creating the test database.
|
33
|
+
# This is necessary if your schema can't be completely dumped by the schema dumper,
|
34
|
+
# like if you have constraints or database-specific column types
|
35
|
+
# config.active_record.schema_format = :sql
|
36
|
+
|
37
|
+
# Activate observers that should always be running
|
38
|
+
# config.active_record.observers = :cacher, :garbage_collector
|
39
|
+
|
40
|
+
# Make Active Record use UTC-base instead of local time
|
41
|
+
# config.active_record.default_timezone = :utc
|
42
|
+
|
43
|
+
# See Rails::Configuration for more options
|
44
|
+
end
|
45
|
+
|
46
|
+
# Add new inflection rules using the following format
|
47
|
+
# (all these examples are active by default):
|
48
|
+
# Inflector.inflections do |inflect|
|
49
|
+
# inflect.plural /^(ox)$/i, '\1en'
|
50
|
+
# inflect.singular /^(ox)en/i, '\1'
|
51
|
+
# inflect.irregular 'person', 'people'
|
52
|
+
# inflect.uncountable %w( fish sheep )
|
53
|
+
# end
|
54
|
+
|
55
|
+
# Include your application configuration below
|
56
|
+
|
57
|
+
module PriceHelper
|
58
|
+
include ActionView::Helpers::NumberHelper
|
59
|
+
def to_currency
|
60
|
+
number_to_currency(self)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class BigDecimal
|
65
|
+
include PriceHelper
|
66
|
+
end
|
67
|
+
|
68
|
+
class Integer
|
69
|
+
include PriceHelper
|
70
|
+
end
|
71
|
+
|
72
|
+
module StringHelper
|
73
|
+
include ActionView::Helpers::TextHelper
|
74
|
+
def truncate
|
75
|
+
super(self, 80).to_s
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class String
|
80
|
+
include StringHelper
|
81
|
+
end
|
82
|
+
|
83
|
+
require 'amrita2/macro'
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
# Settings specified here will take precedence over those in config/environment.rb
|
7
|
+
|
8
|
+
# In the development environment your application's code is reloaded on
|
9
|
+
# every request. This slows down response time but is perfect for development
|
10
|
+
# since you don't have to restart the webserver when you make code changes.
|
11
|
+
config.cache_classes = false
|
12
|
+
|
13
|
+
# Log error messages when you accidentally call methods on nil.
|
14
|
+
config.whiny_nils = true
|
15
|
+
|
16
|
+
# Enable the breakpoint server that script/breakpointer connects to
|
17
|
+
config.breakpoint_server = true
|
18
|
+
|
19
|
+
# Show full error reports and disable caching
|
20
|
+
config.action_controller.consider_all_requests_local = true
|
21
|
+
config.action_controller.perform_caching = false
|
22
|
+
|
23
|
+
# Don't care if the mailer can't send
|
24
|
+
config.action_mailer.raise_delivery_errors = false
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
# Settings specified here will take precedence over those in config/environment.rb
|
7
|
+
|
8
|
+
# The production environment is meant for finished, "live" apps.
|
9
|
+
# Code is not reloaded between requests
|
10
|
+
config.cache_classes = true
|
11
|
+
|
12
|
+
# Use a different logger for distributed setups
|
13
|
+
# config.logger = SyslogLogger.new
|
14
|
+
|
15
|
+
|
16
|
+
# Full error reports are disabled and caching is turned on
|
17
|
+
config.action_controller.consider_all_requests_local = false
|
18
|
+
config.action_controller.perform_caching = true
|
19
|
+
|
20
|
+
# Enable serving of images, stylesheets, and javascripts from an asset server
|
21
|
+
# config.action_controller.asset_host = "http://assets.example.com"
|
22
|
+
|
23
|
+
# Disable delivery errors if you bad email addresses should just be ignored
|
24
|
+
# config.action_mailer.raise_delivery_errors = false
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
# Settings specified here will take precedence over those in config/environment.rb
|
7
|
+
|
8
|
+
# The test environment is used exclusively to run your application's
|
9
|
+
# test suite. You never need to work with it otherwise. Remember that
|
10
|
+
# your test database is "scratch space" for the test suite and is wiped
|
11
|
+
# and recreated between test runs. Don't rely on the data there!
|
12
|
+
config.cache_classes = true
|
13
|
+
|
14
|
+
# Log error messages when you accidentally call methods on nil.
|
15
|
+
config.whiny_nils = true
|
16
|
+
|
17
|
+
# Show full error reports and disable caching
|
18
|
+
config.action_controller.consider_all_requests_local = true
|
19
|
+
config.action_controller.perform_caching = false
|
20
|
+
|
21
|
+
# Tell ActionMailer not to deliver emails to the real world.
|
22
|
+
# The :test delivery method accumulates sent emails in the
|
23
|
+
# ActionMailer::Base.deliveries array.
|
24
|
+
config.action_mailer.delivery_method = :test
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
ActionController::Routing::Routes.draw do |map|
|
7
|
+
map.connect ':controller/service.wsdl', :action => 'wsdl'
|
8
|
+
map.connect ':controller/:action/:id'
|
9
|
+
map.connect ':controller/:action/:id.:format'
|
10
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
class CreateProducts < ActiveRecord::Migration
|
7
|
+
def self.up
|
8
|
+
create_table :products do |t|
|
9
|
+
t.column :title, :string
|
10
|
+
t.column :description, :text
|
11
|
+
t.column :image_url, :string
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.down
|
16
|
+
drop_table :products
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
class AddPrice < ActiveRecord::Migration
|
7
|
+
def self.up
|
8
|
+
add_column :products, :price, :decimal, :precision => 8, :scale => 2, :default => 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
remove_column :products, :price
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
#---
|
2
|
+
# Excerpted from "Agile Web Development with Rails, 2nd Ed."
|
3
|
+
# We make no guarantees that this code is fit for any purpose.
|
4
|
+
# Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.
|
5
|
+
#---
|
6
|
+
|
7
|
+
class AddTestData < ActiveRecord::Migration
|
8
|
+
def self.up
|
9
|
+
Product.delete_all
|
10
|
+
|
11
|
+
Product.create(:title => 'Pragmatic Project Automation',
|
12
|
+
:description =>
|
13
|
+
%{<p>
|
14
|
+
<em>Pragmatic Project Automation</em> shows you how to improve the
|
15
|
+
consistency and repeatability of your project's procedures using
|
16
|
+
automation to reduce risk and errors.
|
17
|
+
</p>
|
18
|
+
<p>
|
19
|
+
Simply put, we're going to put this thing called a computer to work
|
20
|
+
for you doing the mundane (but important) project stuff. That means
|
21
|
+
you'll have more time and energy to do the really
|
22
|
+
exciting---and difficult---stuff, like writing quality code.
|
23
|
+
</p>},
|
24
|
+
:image_url => '/images/auto.jpg',
|
25
|
+
:price => 29.95)
|
26
|
+
|
27
|
+
|
28
|
+
Product.create(:title => 'Pragmatic Version Control',
|
29
|
+
:description =>
|
30
|
+
%{<p>
|
31
|
+
This book is a recipe-based approach to using Subversion that will
|
32
|
+
get you up and
|
33
|
+
running quickly---and correctly. All projects need version control:
|
34
|
+
it's a foundational piece of any project's infrastructure. Yet half
|
35
|
+
of all project teams in the U.S. don't use any version control at all.
|
36
|
+
Many others don't use it well, and end up experiencing time-consuming problems.
|
37
|
+
</p>},
|
38
|
+
:image_url => '/images/svn.jpg',
|
39
|
+
:price => 28.50)
|
40
|
+
# . . .
|
41
|
+
|
42
|
+
|
43
|
+
Product.create(:title => 'Pragmatic Unit Testing (C#)',
|
44
|
+
:description =>
|
45
|
+
%{<p>
|
46
|
+
Pragmatic programmers use feedback to drive their development and
|
47
|
+
personal processes. The most valuable feedback you can get while
|
48
|
+
coding comes from unit testing.
|
49
|
+
</p>
|
50
|
+
<p>
|
51
|
+
Without good tests in place, coding can become a frustrating game of
|
52
|
+
"whack-a-mole." That's the carnival game where the player strikes at a
|
53
|
+
mechanical mole; it retreats and another mole pops up on the opposite side
|
54
|
+
of the field. The moles pop up and down so fast that you end up flailing
|
55
|
+
your mallet helplessly as the moles continue to pop up where you least
|
56
|
+
expect them.
|
57
|
+
</p>},
|
58
|
+
:image_url => '/images/utc.jpg',
|
59
|
+
:price => 27.75)
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
def self.down
|
65
|
+
Product.delete_all
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|