amrita2 1.9.6 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. data/README +112 -0
  2. data/init.rb +6 -0
  3. data/lib/amrita2/gettext.rb +116 -0
  4. data/lib/amrita2/macro.rb +153 -0
  5. data/lib/amrita2/rails_bridge.rb +172 -26
  6. data/lib/amrita2/template.rb +2634 -234
  7. data/lib/amrita2/testsupport.rb +171 -0
  8. data/lib/amrita2/version.rb +3 -3
  9. data/lib/amrita2.rb +1 -0
  10. data/sample/depot/app/controllers/admin_controller.rb +59 -0
  11. data/sample/depot/app/controllers/application.rb +20 -0
  12. data/sample/depot/app/controllers/info_controller.rb +19 -0
  13. data/sample/depot/app/controllers/login_controller.rb +85 -0
  14. data/sample/depot/app/controllers/store_controller.rb +68 -0
  15. data/sample/depot/app/helpers/admin_helper.rb +7 -0
  16. data/sample/depot/app/helpers/application_helper.rb +10 -0
  17. data/sample/depot/app/helpers/ar_form.rb +169 -0
  18. data/sample/depot/app/helpers/form_tag.rb +24 -0
  19. data/sample/depot/app/helpers/info_helper.rb +7 -0
  20. data/sample/depot/app/helpers/standard_form.rb +73 -0
  21. data/sample/depot/app/helpers/store_helper.rb +14 -0
  22. data/sample/depot/app/models/cart.rb +36 -0
  23. data/sample/depot/app/models/cart_item.rb +26 -0
  24. data/sample/depot/app/models/line_item.rb +34 -0
  25. data/sample/depot/app/models/order.rb +57 -0
  26. data/sample/depot/app/models/product.rb +41 -0
  27. data/sample/depot/app/models/user.rb +83 -0
  28. data/sample/depot/config/boot.rb +49 -0
  29. data/sample/depot/config/environment.rb +83 -0
  30. data/sample/depot/config/environments/development.rb +24 -0
  31. data/sample/depot/config/environments/production.rb +24 -0
  32. data/sample/depot/config/environments/test.rb +24 -0
  33. data/sample/depot/config/routes.rb +10 -0
  34. data/sample/depot/db/migrate/001_create_products.rb +18 -0
  35. data/sample/depot/db/migrate/002_add_price.rb +14 -0
  36. data/sample/depot/db/migrate/003_add_test_data.rb +68 -0
  37. data/sample/depot/db/migrate/004_add_sessions.rb +20 -0
  38. data/sample/depot/db/migrate/005_create_orders.rb +21 -0
  39. data/sample/depot/db/migrate/006_create_line_items.rb +27 -0
  40. data/sample/depot/db/migrate/007_create_users.rb +18 -0
  41. data/sample/depot/db/schema.rb +45 -0
  42. data/sample/depot/public/dispatch.rb +15 -0
  43. data/sample/depot/test/functional/admin_controller_test.rb +54 -0
  44. data/sample/depot/test/functional/info_controller_test.rb +23 -0
  45. data/sample/depot/test/functional/login_controller_test.rb +74 -0
  46. data/sample/depot/test/functional/store_controller_test.rb +57 -0
  47. data/sample/depot/test/integration/dsl_user_stories_test.rb +126 -0
  48. data/sample/depot/test/integration/user_stories_test.rb +70 -0
  49. data/sample/depot/test/performance/order_speed_test.rb +58 -0
  50. data/sample/depot/test/test_helper.rb +16 -0
  51. data/sample/depot/test/unit/cart_test.rb +39 -0
  52. data/sample/depot/test/unit/cart_test1.rb +31 -0
  53. data/sample/depot/test/unit/line_item_test.rb +15 -0
  54. data/sample/depot/test/unit/order_test.rb +15 -0
  55. data/sample/depot/test/unit/product_test.rb +98 -0
  56. data/sample/depot/vendor/plugins/amrita2/init.rb +6 -0
  57. data/sample/hello/hello.rb +22 -0
  58. data/sample/login_engine/app/controllers/application.rb +16 -0
  59. data/sample/login_engine/app/controllers/user_controller.rb +265 -0
  60. data/sample/login_engine/app/helpers/application_helper.rb +3 -0
  61. data/sample/login_engine/app/helpers/form_tag.rb +16 -0
  62. data/sample/login_engine/app/helpers/two_columns.rb +24 -0
  63. data/sample/login_engine/app/helpers/two_columns_form.rb +47 -0
  64. data/sample/login_engine/app/helpers/user_helper.rb +88 -0
  65. data/sample/login_engine/app/models/user.rb +7 -0
  66. data/sample/login_engine/app/models/user_notify.rb +75 -0
  67. data/sample/login_engine/config/boot.rb +45 -0
  68. data/sample/login_engine/config/environment.rb +140 -0
  69. data/sample/login_engine/config/environments/development.rb +21 -0
  70. data/sample/login_engine/config/environments/production.rb +18 -0
  71. data/sample/login_engine/config/environments/test.rb +19 -0
  72. data/sample/login_engine/config/routes.rb +23 -0
  73. data/sample/login_engine/db/migrate/001_create_users.rb +25 -0
  74. data/sample/login_engine/db/schema.rb +25 -0
  75. data/sample/login_engine/lib/config.rb +113 -0
  76. data/sample/login_engine/lib/hpricot_test_extension.rb +80 -0
  77. data/sample/login_engine/lib/login_engine/authenticated_system.rb +113 -0
  78. data/sample/login_engine/lib/login_engine/authenticated_user.rb +155 -0
  79. data/sample/login_engine/lib/login_engine.rb +62 -0
  80. data/sample/login_engine/public/dispatch.rb +10 -0
  81. data/sample/login_engine/test/functional/amrita2_test.rb +267 -0
  82. data/sample/login_engine/test/functional/user_controller_test.rb +544 -0
  83. data/sample/login_engine/test/mocks/mail.rb +14 -0
  84. data/sample/login_engine/test/mocks/time.rb +19 -0
  85. data/sample/login_engine/test/test_helper.rb +31 -0
  86. data/sample/login_engine/test/unit/user_test.rb +116 -0
  87. data/sample/login_engine/vendor/plugins/amrita2/init.rb +6 -0
  88. data/specs/attribute.rb +201 -0
  89. data/specs/datatypes.rb +231 -0
  90. data/specs/dictionary.rb +68 -0
  91. data/specs/erb_cdata.rb +187 -0
  92. data/specs/filters.rb +513 -0
  93. data/specs/gettext/erb_gettext.rb +42 -0
  94. data/specs/gettext/gettext_util.rb +65 -0
  95. data/specs/gettext/static_text.rb +103 -0
  96. data/specs/impl/code_generator.rb +87 -0
  97. data/specs/impl/dynamic_element.rb +92 -0
  98. data/specs/impl/hash_delegator.rb +57 -0
  99. data/specs/impl/parse_opt.rb +34 -0
  100. data/specs/impl/preprocess.rb +823 -0
  101. data/specs/impl/testsupport.rb +89 -0
  102. data/specs/inlineruby.rb +429 -0
  103. data/specs/intro.rb +654 -0
  104. data/specs/loop.rb +203 -0
  105. data/specs/macro.rb +532 -0
  106. data/specs/sample.rb +34 -0
  107. data/specs/sanitize.rb +110 -0
  108. data/specs/template.rb +189 -0
  109. data/specs/trace.rb +97 -0
  110. metadata +138 -19
  111. data/lib/amrita2/core.rb +0 -1897
  112. data/lib/amrita2/rd.rb +0 -314
@@ -0,0 +1,31 @@
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
+ require File.dirname(__FILE__) + '/../test_helper'
7
+
8
+ class CartTest < Test::Unit::TestCase
9
+ fixtures :products
10
+
11
+ def setup
12
+ @cart = Cart.new
13
+ @rails = products(:rails_book)
14
+ @ruby = products(:ruby_book)
15
+ end
16
+
17
+ def test_add_unique_products
18
+ @cart.add_product @rails
19
+ @cart.add_product @ruby
20
+ assert_equal 2, @cart.items.size
21
+ assert_equal @rails.price + @ruby.price, @cart.total_price
22
+ end
23
+
24
+ def test_add_duplicate_product
25
+ @cart.add_product @rails
26
+ @cart.add_product @rails
27
+ assert_equal 2*@rails.price, @cart.total_price
28
+ assert_equal 1, @cart.items.size
29
+ assert_equal 2, @cart.items[0].quantity
30
+ end
31
+ end
@@ -0,0 +1,15 @@
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
+ require File.dirname(__FILE__) + '/../test_helper'
7
+
8
+ class LineItemTest < Test::Unit::TestCase
9
+ fixtures :line_items
10
+
11
+ # Replace this with your real tests.
12
+ def test_truth
13
+ assert true
14
+ end
15
+ end
@@ -0,0 +1,15 @@
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
+ require File.dirname(__FILE__) + '/../test_helper'
7
+
8
+ class OrderTest < Test::Unit::TestCase
9
+ fixtures :orders
10
+
11
+ # Replace this with your real tests.
12
+ def test_truth
13
+ assert true
14
+ end
15
+ end
@@ -0,0 +1,98 @@
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
+ require File.dirname(__FILE__) + '/../test_helper'
8
+
9
+ class ProductTest < Test::Unit::TestCase
10
+
11
+
12
+ fixtures :products
13
+
14
+
15
+ def test_truth
16
+ assert true
17
+ end
18
+
19
+
20
+
21
+ def test_invalid_with_empty_attributes
22
+ product = Product.new
23
+ assert !product.valid?
24
+ assert product.errors.invalid?(:title)
25
+ assert product.errors.invalid?(:description)
26
+ assert product.errors.invalid?(:price)
27
+ assert product.errors.invalid?(:image_url)
28
+ end
29
+
30
+
31
+
32
+ def test_positive_price
33
+ product = Product.new(:title => "My Book Title",
34
+ :description => "yyy",
35
+ :image_url => "zzz.jpg")
36
+ product.price = -1
37
+ assert !product.valid?
38
+ assert_equal "should be at least 0.01", product.errors.on(:price)
39
+
40
+ product.price = 0
41
+ assert !product.valid?
42
+ assert_equal "should be at least 0.01", product.errors.on(:price)
43
+
44
+ product.price = 1
45
+ assert product.valid?
46
+ end
47
+
48
+
49
+
50
+ def test_image_url
51
+ ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg
52
+ http://a.b.c/x/y/z/fred.gif }
53
+ bad = %w{ fred.doc fred.gif/more fred.gif.more }
54
+
55
+ ok.each do |name|
56
+ product = Product.new(:title => "My Book Title",
57
+ :description => "yyy",
58
+ :price => 1,
59
+ :image_url => name)
60
+ assert product.valid?, product.errors.full_messages
61
+ end
62
+
63
+ bad.each do |name|
64
+ product = Product.new(:title => "My Book Title", :description => "yyy", :price => 1,
65
+ :image_url => name)
66
+ assert !product.valid?, "saving #{name}"
67
+ end
68
+ end
69
+
70
+
71
+
72
+ def test_unique_title
73
+ product = Product.new(:title => products(:ruby_book).title,
74
+ :description => "yyy",
75
+ :price => 1,
76
+ :image_url => "fred.gif")
77
+
78
+ assert !product.save
79
+ assert_equal "has already been taken", product.errors.on(:title)
80
+ end
81
+
82
+
83
+
84
+ def test_unique_title1
85
+ product = Product.new(:title => products(:ruby_book).title,
86
+ :description => "yyy",
87
+ :price => 1,
88
+ :image_url => "fred.gif")
89
+
90
+ assert !product.save
91
+ assert_equal ActiveRecord::Errors.default_error_messages[:taken],
92
+ product.errors.on(:title)
93
+ end
94
+
95
+
96
+
97
+ end
98
+
@@ -0,0 +1,6 @@
1
+ # plugin init files for Ruby On Rails
2
+
3
+ require 'amrita2/rails_bridge'
4
+
5
+ ActionView::Base.register_template_handler "a2html", Amrita2View::Base
6
+
@@ -0,0 +1,22 @@
1
+ require "amrita2/template"
2
+ include Amrita2
3
+
4
+ tmpl = Template.new <<-END
5
+ <<html<
6
+ <<body<
7
+ <<h1 :title>>
8
+ <<p :body<
9
+ <<:template>> is a html template libraly for <<:lang>>
10
+ END
11
+
12
+ #tmpl.set_trace(STDOUT)
13
+ puts tmpl.render_with(:title=>"hello world", :body=>{ :template=>"Amrita2", :lang=>"Ruby" })
14
+
15
+ __END__
16
+
17
+ <html>
18
+ <body>
19
+ <h1>hello world</h1>
20
+ <p>Amrita2 is a html template libraly for Ruby</p>
21
+ </body>
22
+ </html>
@@ -0,0 +1,16 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ include LoginEngine
6
+
7
+ #GetText.locale = "ja"
8
+ init_gettext "login_engine"
9
+
10
+ before_filter :login_required
11
+
12
+ # Pick a unique cookie name to distinguish our session data from others'
13
+ session :session_key => '_lymr_session_id'
14
+
15
+
16
+ end
@@ -0,0 +1,265 @@
1
+ class UserController < ApplicationController
2
+
3
+ # Override this function in your own application to define a custom home action.
4
+ def home
5
+ if user?
6
+ @fullname = "#{current_user.firstname} #{current_user.lastname}"
7
+ else
8
+ @fullname = "Not logged in..."
9
+ end # this is a bit of a hack since the home action is used to verify user
10
+ # keys, where noone is logged in. We should probably create a unique
11
+ # 'validate_key' action instead.
12
+ end
13
+
14
+ # The action used to log a user in. If the user was redirected to the login page
15
+ # by the login_required method, they should be sent back to the page they were
16
+ # trying to access. If not, they will be sent to "/user/home".
17
+ def login
18
+ return if generate_blank
19
+ @user = User.new(params[:user])
20
+ if session[:user] = User.authenticate(params[:user][:login], params[:user][:password])
21
+ session[:user].logged_in_at = Time.now
22
+ session[:user].save
23
+ flash[:notice] = _('Login successful')
24
+ redirect_to_stored_or_default :action => 'home'
25
+ else
26
+ @login = params[:user][:login]
27
+ flash.now[:warning] = _('Login unsuccessful')
28
+ end
29
+ end
30
+
31
+ # Register as a new user. Upon successful registration, the user will be sent to
32
+ # "/user/login" to enter their details.
33
+ def signup
34
+ return if generate_blank
35
+ params[:user].delete('form')
36
+ params[:user].delete('verified') # you CANNOT pass this as part of the request
37
+ @user = User.new(params[:user])
38
+ begin
39
+ #User.transaction(@user) do
40
+ User.transaction do
41
+ @user.new_password = true
42
+ unless LoginEngine.config(:use_email_notification) and LoginEngine.config(:confirm_account)
43
+ @user.verified = 1
44
+ end
45
+ if @user.save
46
+ key = @user.generate_security_token
47
+ url = url_for(:action => 'home', :user_id => @user.id, :key => key)
48
+ flash[:notice] = 'Signup successful!'
49
+ if LoginEngine.config(:use_email_notification) and LoginEngine.config(:confirm_account)
50
+ UserNotify.deliver_signup(@user, params[:user][:password], url)
51
+ flash[:notice] << _(' Please check your registered email account to verify your account registration and continue with the login.')
52
+ else
53
+ flash[:notice] << _(' Please log in.')
54
+ end
55
+ redirect_to :action => 'login'
56
+ end
57
+ end
58
+ rescue Exception => e
59
+ flash.now[:notice] = nil
60
+ flash.now[:warning] = _('Error creating account: confirmation email not sent')
61
+ logger.error "Unable to send confirmation E-Mail:"
62
+ logger.error e
63
+ end
64
+ end
65
+
66
+ def logout
67
+ session[:user] = nil
68
+ redirect_to :action => 'login'
69
+ end
70
+
71
+ def change_password
72
+ return if generate_filled_in
73
+ if do_change_password_for(@user)
74
+ # since sometimes we're changing the password from within another action/template...
75
+ #redirect_to :action => params[:back_to] if params[:back_to]
76
+ redirect_back_or_default :action => 'change_password'
77
+ end
78
+ end
79
+
80
+ protected
81
+ def do_change_password_for(user)
82
+ begin
83
+ #User.transaction(user) do
84
+ User.transaction do
85
+ user.change_password(params[:user][:password], params[:user][:password_confirmation])
86
+ if user.save
87
+ if LoginEngine.config(:use_email_notification)
88
+ UserNotify.deliver_change_password(user, params[:user][:password])
89
+ flash[:notice] = "Updated password emailed to #{@user.email}"
90
+ else
91
+ flash[:notice] = _("Password updated.")
92
+ end
93
+ return true
94
+ else
95
+ flash[:warning] = _('There was a problem saving the password. Please retry.')
96
+ return false
97
+ end
98
+ end
99
+ rescue
100
+ flash[:warning] = _('Password could not be changed at this time. Please retry.')
101
+ end
102
+ end
103
+
104
+ public
105
+
106
+
107
+ def forgot_password
108
+ # Always redirect if logged in
109
+ if user?
110
+ flash[:message] = _('You are currently logged in. You may change your password now.')
111
+ redirect_to :action => 'change_password'
112
+ return
113
+ end
114
+
115
+ # Email disabled... we are unable to provide the password
116
+ if !LoginEngine.config(:use_email_notification)
117
+ flash[:message] = "Please contact the system admin at #{LoginEngine.config(:admin_email)} to reset your password."
118
+ redirect_back_or_default :action => 'login'
119
+ return
120
+ end
121
+
122
+ # Render on :get and render
123
+ return if generate_blank
124
+
125
+ # Handle the :post
126
+ if params[:user][:email].empty?
127
+ flash.now[:warning] = _('Please enter a valid email address.')
128
+ elsif (user = User.find_by_email(params[:user][:email])).nil?
129
+ flash.now[:warning] = "We could not find a user with the email address #{params[:user][:email]}"
130
+ else
131
+ begin
132
+ #User.transaction(user) do
133
+ User.transaction do
134
+ key = user.generate_security_token
135
+ url = url_for(:action => 'change_password', :user_id => user.id, :key => key)
136
+ UserNotify.deliver_forgot_password(user, url)
137
+ flash[:notice] = "Instructions on resetting your password have been emailed to #{params[:user][:email]}"
138
+ end
139
+ unless user?
140
+ redirect_to :action => 'login'
141
+ return
142
+ end
143
+ redirect_back_or_default :action => 'home'
144
+ rescue
145
+ flash.now[:warning] = "Your password could not be emailed to #{params[:user][:email]}"
146
+ end
147
+ end
148
+ end
149
+
150
+ def edit
151
+ return if generate_filled_in
152
+ do_edit_user(@user)
153
+ end
154
+
155
+ protected
156
+ def do_edit_user(user)
157
+ begin
158
+ #User.transaction(user) do
159
+ User.transaction do
160
+ user.attributes = params[:user].delete_if { |k,v| not LoginEngine.config(:changeable_fields).include?(k) }
161
+ if user.save
162
+ flash[:notice] = _("User details updated")
163
+ else
164
+ flash[:warning] = _("Details could not be updated! Please retry.")
165
+ end
166
+ end
167
+ rescue
168
+ flash.now[:warning] = _("Error updating user details. Please try again later.")
169
+ end
170
+ end
171
+
172
+ public
173
+
174
+ def delete
175
+ get_user_to_act_on
176
+ if do_delete_user(@user)
177
+ logout
178
+ else
179
+ redirect_back_or_default :action => 'home'
180
+ end
181
+ end
182
+
183
+ protected
184
+ def do_delete_user(user)
185
+ begin
186
+ if LoginEngine.config(:delayed_delete)
187
+ #User.transaction(user) do
188
+ User.transaction do
189
+ key = user.set_delete_after
190
+ if LoginEngine.config(:use_email_notification)
191
+ url = url_for(:action => 'restore_deleted', :user_id => user.id, :key => key)
192
+ UserNotify.deliver_pending_delete(user, url)
193
+ end
194
+ end
195
+ else
196
+ destroy(@user)
197
+ end
198
+ return true
199
+ rescue
200
+ if LoginEngine.config(:use_email_notification)
201
+ flash.now[:warning] = _('The delete instructions were not sent. Please try again later.')
202
+ else
203
+ flash.now[:notice] = 'The account has been scheduled for deletion. It will be removed in #{LoginEngine.config(:delayed_delete_days)} days.'
204
+ end
205
+ return false
206
+ end
207
+ end
208
+
209
+ public
210
+
211
+ def restore_deleted
212
+ get_user_to_act_on
213
+ @user.deleted = 0
214
+ if not @user.save
215
+ flash.now[:warning] = "The account for #{@user['login']} was not restored. Please try the link again."
216
+ redirect_to :action => 'login'
217
+ else
218
+ redirect_to :action => 'home'
219
+ end
220
+ end
221
+
222
+ protected
223
+
224
+ def destroy(user)
225
+ UserNotify.deliver_delete(user) if LoginEngine.config(:use_email_notification)
226
+ flash[:notice] = "The account for #{user['login']} was successfully deleted."
227
+ user.destroy()
228
+ end
229
+
230
+ def protect?(action)
231
+ if ['login', 'signup', 'forgot_password'].include?(action)
232
+ return false
233
+ else
234
+ return true
235
+ end
236
+ end
237
+
238
+ # Generate a template user for certain actions on get
239
+ def generate_blank
240
+ case request.method
241
+ when :get
242
+ @user = User.new
243
+ render
244
+ return true
245
+ end
246
+ return false
247
+ end
248
+
249
+ # Generate a template user for certain actions on get
250
+ def generate_filled_in
251
+ get_user_to_act_on
252
+ case request.method
253
+ when :get
254
+ render
255
+ return true
256
+ end
257
+ return false
258
+ end
259
+
260
+ # returns the user object this method should act upon; only really
261
+ # exists for other engines operating on top of this one to redefine...
262
+ def get_user_to_act_on
263
+ @user = session[:user]
264
+ end
265
+ end
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,16 @@
1
+ class FormTag < Amrita2::Macro::Base
2
+ TemplateText = <<-END
3
+ <%
4
+ action = $_[:action]
5
+ method = $_[:method]
6
+ %>
7
+ <%%= form_tag({:action=><%= action.inspect%>}, {:method=><%= method.inspect %>}) %%>
8
+ <span macro:src='contents' />
9
+ <%%= "</form>" %%>
10
+ END
11
+
12
+ Option = {
13
+ :tag => :form_tag,
14
+ :use_contents => :contents
15
+ }
16
+ end
@@ -0,0 +1,24 @@
1
+
2
+ class TwoColumns < Amrita2::Macro::Base
3
+ TemplateText = <<-END
4
+ <<table<
5
+ <<tr class="two_columns":rows<
6
+ <<td class="prompt"<
7
+ <<label:title>>
8
+ <<td class="value" :contents>>
9
+ END
10
+
11
+ def macro_data(element)
12
+ root = element.as_amrita_dictionary
13
+ rows = element.search("tr").collect do |c|
14
+ title, contents = *c.search("td")
15
+ {
16
+ :title => title.contents,
17
+ :contents => Amrita2::SanitizedString[contents.children.to_s],
18
+ }
19
+ end
20
+ {
21
+ :rows => rows,
22
+ }
23
+ end
24
+ end
@@ -0,0 +1,47 @@
1
+
2
+ class TwoColumnsForm < Amrita2::Macro::Base
3
+ TemplateText = <<-END
4
+ <<div class="form-padding"<
5
+ <%%
6
+ action = <%= $_[:form] && $_[:form][:action] ? $_[:form][:action].inspect : "url_for" %>
7
+ method = <%= $_[:form] && $_[:form][:method] ? $_[:form][:method].inspect : "post".inspect %>
8
+ %%>
9
+ <<form action="$1" method="$2"
10
+ target_filter="NVarForAttr[:action, :method]"<
11
+ <<table<
12
+ <<tr macro:src="rows"
13
+ macro:filter="Attr[:class=>:tr_class]"<
14
+ <<td macro:filter="Attr[:class=>:prompt_class]"<
15
+ <label macro:src="title" />
16
+ <td macro:filter="Attr[:class=>:value_class, :body=>:contents]" />
17
+ <<div class="button-bar"<
18
+ <span macro:src="button_bar">>
19
+ END
20
+
21
+ def macro_data(element)
22
+ root = element.as_amrita_dictionary
23
+ form_element = element.search("form").first
24
+ form = form_element.as_amrita_dictionary if form_element
25
+ rows = element.search("tr").collect do |c|
26
+ title, contents = *c.search("td")
27
+ {
28
+ :tr_class => root[:tr_class] || "two_columns",
29
+ :prompt_class => root[:prompt_class] || "prompt",
30
+ :title => title.children.to_s,
31
+ :value_class => root[:value_class] || "value",
32
+ :contents => Amrita2::SanitizedString[contents.children.to_s],
33
+ }
34
+ end
35
+ {
36
+ :form => form,
37
+ :rows => rows,
38
+ :button_bar => Amrita2::SanitizedString[element.search("button_bar").first.children.to_s]
39
+ }
40
+ end
41
+
42
+ #def preprocess_element(mt, element)
43
+ ##mt.set_trace(STDOUT)
44
+ ##p macro_data(element)
45
+ #mt.render_with(macro_data(element))
46
+ #end
47
+ end
@@ -0,0 +1,88 @@
1
+ module UserHelper
2
+
3
+ # Abstraction to make views a little cleaner
4
+ def form_input(helper_method, prompt, field_name=nil, options = {}, form_name = nil)
5
+ form_name = "user" if form_name.nil?
6
+ case helper_method.to_s
7
+ when 'hidden_field'
8
+ self.hidden_field(form_name, field_name, options)
9
+ when /^.*button$/
10
+ #prompt = l(:"#{@controller.controller_name}_#{field_name}_button")
11
+ <<-EOL
12
+ <tr><td class="button" colspan="2">
13
+ #{self.send(helper_method, form_name, prompt, options)}
14
+ </td></tr>
15
+ EOL
16
+ else
17
+ field = (
18
+ case helper_method
19
+ when :select
20
+ self.send(helper_method, form_name, field_name, options.delete('values'), options)
21
+ when :password_field
22
+ options[:value] = ""
23
+ self.send(helper_method, form_name, field_name, options)
24
+ else
25
+ self.send(helper_method, form_name, field_name, options)
26
+ end)
27
+ # lname = "#{form_name}_#{field_name}_form"
28
+ # prompt = l(:"#{lname}")
29
+ if LoginEngine.config(:two_column_input)
30
+ <<-EOL
31
+ <tr class="two_columns">
32
+ <td class="prompt"><label>#{prompt}:</label></td>
33
+ <td class="value">#{field}</td>
34
+ </tr>
35
+ EOL
36
+ else
37
+ <<-EOL
38
+ <tr><td class="prompt"><label>#{prompt}:</label></td></tr>
39
+ <tr><td class="value">#{field}</td></tr>
40
+ EOL
41
+ end
42
+ end
43
+ end
44
+
45
+ # def button_helper(name, options = {})
46
+ # label = l(:"#{@controller.controller_name}_#{name}_button")
47
+ # "#{self.send(:submit_tag, label, options)}"
48
+ # end
49
+
50
+ # def link_helper(name, options = {})
51
+ # raise ArgumentError if name.nil?
52
+ # label = l(:"#{@controller.controller_name}_#{name}_link")
53
+ # "#{self.send(:link_to, label, options)}"
54
+ # end
55
+
56
+ def title_helper
57
+ "#{@controller.controller_class_name} #{@controller.action_name}"
58
+ end
59
+
60
+ # def message_helper(name)
61
+ # l(:"#{@controller.controller_name}_#{name}_message")
62
+ # end
63
+
64
+ def start_form_tag_helper(options = {})
65
+ url = url_for(:action => "#{@controller.action_name}")
66
+ "#{self.send(:start_form_tag, url, options)}"
67
+ end
68
+
69
+ def attributes(hash)
70
+ hash.keys.inject("") { |attrs, key| attrs + %{#{key}="#{h(hash[key])}" } }
71
+ end
72
+
73
+ def read_only_field(form_name, field_name, html_options)
74
+ "<span #{attributes(html_options)}>#{instance_variable_get('@' + form_name)[field_name]}</span>"
75
+ end
76
+
77
+ def submit_button(form_name, prompt, html_options)
78
+ %{<input name="submit" type="submit" value="#{prompt}" />}
79
+ end
80
+
81
+ def changeable(user, field)
82
+ if user.new_record? or LoginEngine.config(:changeable_fields).include?(field)
83
+ :text_field
84
+ else
85
+ :read_only_field
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,7 @@
1
+ class User < ActiveRecord::Base
2
+ include LoginEngine::AuthenticatedUser
3
+
4
+ # all logic has been moved into login_engine/lib/login_engine/authenticated_user.rb
5
+
6
+ end
7
+