tournament 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +15 -0
- data/Manifest.txt +241 -2
- data/README.txt +152 -12
- data/Rakefile +9 -6
- data/bin/benchmark_pool +0 -0
- data/bin/gui.rb +3 -1
- data/bin/picker +0 -0
- data/bin/pool +139 -15
- data/lib/tournament/bracket.rb +33 -50
- data/lib/tournament/entry.rb +3 -0
- data/lib/tournament/pool.rb +144 -93
- data/lib/tournament/scoring_strategy.rb +69 -0
- data/lib/tournament/webgui_installer.rb +109 -0
- data/tasks/ann.rake +19 -14
- data/tasks/bones.rake +6 -25
- data/tasks/gem.rake +46 -36
- data/tasks/git.rake +41 -0
- data/tasks/jamis.rb +589 -0
- data/tasks/notes.rake +28 -0
- data/tasks/post_load.rake +18 -11
- data/tasks/{doc.rake → rdoc.rake} +12 -9
- data/tasks/rubyforge.rake +12 -12
- data/tasks/setup.rb +121 -80
- data/tasks/spec.rake +12 -13
- data/tasks/svn.rake +15 -11
- data/tasks/test.rake +6 -6
- data/test/test_tournament.rb +8 -0
- data/test/test_webgui_installer.rb +64 -0
- data/webgui/README +256 -0
- data/webgui/Rakefile +10 -0
- data/webgui/app/controllers/admin_controller.rb +61 -0
- data/webgui/app/controllers/application.rb +25 -0
- data/webgui/app/controllers/entry_controller.rb +97 -0
- data/webgui/app/controllers/pool_controller.rb +4 -0
- data/webgui/app/controllers/reports_controller.rb +7 -0
- data/webgui/app/controllers/sessions_controller.rb +43 -0
- data/webgui/app/controllers/teams_controller.rb +75 -0
- data/webgui/app/controllers/users_controller.rb +57 -0
- data/webgui/app/helpers/admin_helper.rb +2 -0
- data/webgui/app/helpers/application_helper.rb +3 -0
- data/webgui/app/helpers/entry_helper.rb +2 -0
- data/webgui/app/helpers/pool_helper.rb +2 -0
- data/webgui/app/helpers/reports_helper.rb +2 -0
- data/webgui/app/helpers/sessions_helper.rb +2 -0
- data/webgui/app/helpers/teams_helper.rb +2 -0
- data/webgui/app/helpers/users_helper.rb +93 -0
- data/webgui/app/models/entry.rb +42 -0
- data/webgui/app/models/pool.rb +141 -0
- data/webgui/app/models/region.rb +3 -0
- data/webgui/app/models/role.rb +4 -0
- data/webgui/app/models/seeding.rb +4 -0
- data/webgui/app/models/team.rb +6 -0
- data/webgui/app/models/user.rb +98 -0
- data/webgui/app/models/user_mailer.rb +25 -0
- data/webgui/app/views/admin/bracket.html.erb +3 -0
- data/webgui/app/views/admin/entries.html.erb +25 -0
- data/webgui/app/views/admin/index.html.erb +17 -0
- data/webgui/app/views/admin/pool.html.erb +53 -0
- data/webgui/app/views/entry/index.html.erb +16 -0
- data/webgui/app/views/entry/print.erb +1 -0
- data/webgui/app/views/entry/print.html.erb +1 -0
- data/webgui/app/views/entry/show.html.erb +1 -0
- data/webgui/app/views/layouts/bracket.html.erb +37 -0
- data/webgui/app/views/layouts/default.html.erb +62 -0
- data/webgui/app/views/layouts/print.html.erb +12 -0
- data/webgui/app/views/layouts/report.html.erb +38 -0
- data/webgui/app/views/pool/index.html.erb +36 -0
- data/webgui/app/views/reports/_report.html.erb +11 -0
- data/webgui/app/views/reports/show.html.erb +12 -0
- data/webgui/app/views/sessions/new.html.erb +21 -0
- data/webgui/app/views/shared/_admins.html.erb +8 -0
- data/webgui/app/views/shared/_bracket.html.erb +1296 -0
- data/webgui/app/views/teams/choose.html.erb +57 -0
- data/webgui/app/views/user_mailer/activation.erb +3 -0
- data/webgui/app/views/user_mailer/signup_notification.erb +8 -0
- data/webgui/app/views/users/_user_bar.html.erb +8 -0
- data/webgui/app/views/users/new.html.erb +19 -0
- data/webgui/config/boot.rb +109 -0
- data/webgui/config/database.yml +22 -0
- data/webgui/config/environment.rb +78 -0
- data/webgui/config/environments/development.rb +29 -0
- data/webgui/config/environments/production.rb +24 -0
- data/webgui/config/environments/test.rb +22 -0
- data/webgui/config/initializers/inflections.rb +10 -0
- data/webgui/config/initializers/mime_types.rb +5 -0
- data/webgui/config/initializers/new_rails_defaults.rb +17 -0
- data/webgui/config/initializers/pool.rb +33 -0
- data/webgui/config/initializers/site_keys.rb +38 -0
- data/webgui/config/locales/en.yml +5 -0
- data/webgui/config/routes.rb +56 -0
- data/webgui/db/development.sqlite3 +0 -0
- data/webgui/db/migrate/20090216015836_create_entries.rb +13 -0
- data/webgui/db/migrate/20090217001611_add_tie_break_to_entry.rb +9 -0
- data/webgui/db/migrate/20090217004039_create_users.rb +23 -0
- data/webgui/db/migrate/20090217014940_relate_entry_and_user.rb +10 -0
- data/webgui/db/migrate/20090218024350_create_roles.rb +17 -0
- data/webgui/db/migrate/20090218024657_add_postition_to_roles.rb +9 -0
- data/webgui/db/migrate/20090218025240_add_admin_role.rb +10 -0
- data/webgui/db/migrate/20090225022928_create_pools.rb +16 -0
- data/webgui/db/migrate/20090225030346_associate_pool_and_entry.rb +9 -0
- data/webgui/db/migrate/20090225072358_change_pool_data_column.rb +9 -0
- data/webgui/db/migrate/20090225072849_add_completed_to_entries.rb +9 -0
- data/webgui/db/migrate/20090225074149_add_pool_id_index_to_entries.rb +9 -0
- data/webgui/db/migrate/20090225084414_add_user_id_to_pool.rb +9 -0
- data/webgui/db/migrate/20090226021913_add_active_to_pools.rb +9 -0
- data/webgui/db/migrate/20090226142240_rename_bracket_column_to_data.rb +9 -0
- data/webgui/db/migrate/20090301014710_create_teams.rb +84 -0
- data/webgui/db/migrate/20090301025902_add_more_teams.rb +16 -0
- data/webgui/db/migrate/20090301041526_create_seedings.rb +16 -0
- data/webgui/db/migrate/20090301090511_create_regions.rb +14 -0
- data/webgui/db/migrate/teams.txt +347 -0
- data/webgui/db/pool.yml +408 -0
- data/webgui/db/schema.rb +91 -0
- data/webgui/db/test.sqlite3 +0 -0
- data/webgui/doc/README_FOR_APP +5 -0
- data/webgui/lib/authenticated_system.rb +189 -0
- data/webgui/lib/authenticated_test_helper.rb +11 -0
- data/webgui/lib/form_object.rb +70 -0
- data/webgui/lib/pdf_helper.rb +47 -0
- data/webgui/lib/prince.rb +74 -0
- data/webgui/lib/saves_picks.rb +23 -0
- data/webgui/lib/tasks/admin.rake +10 -0
- data/webgui/public/404.html +30 -0
- data/webgui/public/422.html +30 -0
- data/webgui/public/500.html +33 -0
- data/webgui/public/dispatch.cgi +10 -0
- data/webgui/public/dispatch.fcgi +24 -0
- data/webgui/public/dispatch.rb +10 -0
- data/webgui/public/favicon.ico +0 -0
- data/webgui/public/images/2009FinalFour.png +0 -0
- data/webgui/public/images/rails.png +0 -0
- data/webgui/public/javascripts/application.js +2 -0
- data/webgui/public/javascripts/bracket.js +73 -0
- data/webgui/public/javascripts/controls.js +963 -0
- data/webgui/public/javascripts/dragdrop.js +973 -0
- data/webgui/public/javascripts/effects.js +1128 -0
- data/webgui/public/javascripts/prototype.js +4320 -0
- data/webgui/public/robots.txt +5 -0
- data/webgui/public/stylesheets/bracket-print.css +39 -0
- data/webgui/public/stylesheets/bracket.css +125 -0
- data/webgui/public/stylesheets/main.css +144 -0
- data/webgui/public/stylesheets/prince.css +6 -0
- data/webgui/public/stylesheets/prince_landscape.css +1 -0
- data/webgui/script/about +4 -0
- data/webgui/script/console +3 -0
- data/webgui/script/dbconsole +3 -0
- data/webgui/script/destroy +3 -0
- data/webgui/script/generate +3 -0
- data/webgui/script/performance/benchmarker +3 -0
- data/webgui/script/performance/profiler +3 -0
- data/webgui/script/performance/request +3 -0
- data/webgui/script/plugin +3 -0
- data/webgui/script/process/inspector +3 -0
- data/webgui/script/process/reaper +3 -0
- data/webgui/script/process/spawner +3 -0
- data/webgui/script/runner +3 -0
- data/webgui/script/server +3 -0
- data/webgui/test/fixtures/entries.yml +10 -0
- data/webgui/test/fixtures/pools.yml +14 -0
- data/webgui/test/fixtures/regions.yml +11 -0
- data/webgui/test/fixtures/roles.yml +11 -0
- data/webgui/test/fixtures/roles_users.yml +3 -0
- data/webgui/test/fixtures/seedings.yml +12 -0
- data/webgui/test/fixtures/teams.yml +3051 -0
- data/webgui/test/fixtures/users.yml +44 -0
- data/webgui/test/functional/admin_controller_test.rb +8 -0
- data/webgui/test/functional/entry_controller_test.rb +27 -0
- data/webgui/test/functional/pool_controller_test.rb +8 -0
- data/webgui/test/functional/reports_controller_test.rb +8 -0
- data/webgui/test/functional/sessions_controller_test.rb +82 -0
- data/webgui/test/functional/teams_controller_test.rb +216 -0
- data/webgui/test/functional/users_controller_test.rb +88 -0
- data/webgui/test/performance/browsing_test.rb +9 -0
- data/webgui/test/test_helper.rb +38 -0
- data/webgui/test/unit/entry_test.rb +8 -0
- data/webgui/test/unit/pool_test.rb +8 -0
- data/webgui/test/unit/region_test.rb +8 -0
- data/webgui/test/unit/role_test.rb +8 -0
- data/webgui/test/unit/seeding_test.rb +8 -0
- data/webgui/test/unit/team_test.rb +10 -0
- data/webgui/test/unit/user_mailer_test.rb +31 -0
- data/webgui/test/unit/user_test.rb +109 -0
- data/webgui/vendor/plugins/auto_complete/README +23 -0
- data/webgui/vendor/plugins/auto_complete/Rakefile +22 -0
- data/webgui/vendor/plugins/auto_complete/init.rb +2 -0
- data/webgui/vendor/plugins/auto_complete/lib/auto_complete.rb +47 -0
- data/webgui/vendor/plugins/auto_complete/lib/auto_complete_macros_helper.rb +143 -0
- data/webgui/vendor/plugins/auto_complete/test/auto_complete_test.rb +67 -0
- data/webgui/vendor/plugins/enumerations_mixin/ATTENTION +15 -0
- data/webgui/vendor/plugins/enumerations_mixin/LICENSE +20 -0
- data/webgui/vendor/plugins/enumerations_mixin/README_ENUMERATIONS +163 -0
- data/webgui/vendor/plugins/enumerations_mixin/init.rb +21 -0
- data/webgui/vendor/plugins/enumerations_mixin/lib/active_record/acts/enumerated.rb +177 -0
- data/webgui/vendor/plugins/enumerations_mixin/lib/active_record/aggregations/has_enumerated.rb +60 -0
- data/webgui/vendor/plugins/enumerations_mixin/lib/active_record/virtual_enumerations.rb +68 -0
- data/webgui/vendor/plugins/enumerations_mixin/virtual_enumerations_sample.rb +76 -0
- data/webgui/vendor/plugins/restful_authentication/.gitignore +21 -0
- data/webgui/vendor/plugins/restful_authentication/CHANGELOG +68 -0
- data/webgui/vendor/plugins/restful_authentication/README.textile +224 -0
- data/webgui/vendor/plugins/restful_authentication/Rakefile +32 -0
- data/webgui/vendor/plugins/restful_authentication/TODO +15 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/USAGE +1 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb +478 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/lib/insert_routes.rb +54 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/_model_partial.html.erb +8 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/activation.erb +3 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/authenticated_system.rb +189 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/authenticated_test_helper.rb +22 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/controller.rb +43 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/helper.rb +2 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/login.html.erb +16 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/mailer.rb +25 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/migration.rb +26 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/model.rb +83 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/model_controller.rb +85 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/model_helper.rb +93 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/model_helper_spec.rb +158 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/observer.rb +11 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/signup.html.erb +19 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/signup_notification.erb +8 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/site_keys.rb +38 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/spec/controllers/access_control_spec.rb +90 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/spec/controllers/authenticated_system_spec.rb +102 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/spec/controllers/sessions_controller_spec.rb +139 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/spec/controllers/users_controller_spec.rb +198 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/spec/fixtures/users.yml +60 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/spec/helpers/users_helper_spec.rb +141 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/spec/models/user_spec.rb +290 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/stories/rest_auth_stories.rb +22 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/stories/rest_auth_stories_helper.rb +81 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/stories/steps/ra_navigation_steps.rb +49 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/stories/steps/ra_resource_steps.rb +179 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/stories/steps/ra_response_steps.rb +171 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/stories/steps/user_steps.rb +153 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/stories/users/accounts.story +186 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/stories/users/sessions.story +134 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/test/functional_test.rb +82 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/test/mailer_test.rb +31 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/test/model_functional_test.rb +93 -0
- data/webgui/vendor/plugins/restful_authentication/generators/authenticated/templates/test/unit_test.rb +164 -0
- data/webgui/vendor/plugins/restful_authentication/init.rb +1 -0
- data/webgui/vendor/plugins/restful_authentication/install.rb +1 -0
- data/webgui/vendor/plugins/restful_authentication/lib/authentication/by_cookie_token.rb +82 -0
- data/webgui/vendor/plugins/restful_authentication/lib/authentication/by_password.rb +64 -0
- data/webgui/vendor/plugins/restful_authentication/lib/authentication.rb +40 -0
- data/webgui/vendor/plugins/restful_authentication/lib/authorization/aasm_roles.rb +63 -0
- data/webgui/vendor/plugins/restful_authentication/lib/authorization/stateful_roles.rb +62 -0
- data/webgui/vendor/plugins/restful_authentication/lib/authorization.rb +14 -0
- data/webgui/vendor/plugins/restful_authentication/lib/trustification/email_validation.rb +20 -0
- data/webgui/vendor/plugins/restful_authentication/lib/trustification.rb +14 -0
- data/webgui/vendor/plugins/restful_authentication/notes/AccessControl.txt +2 -0
- data/webgui/vendor/plugins/restful_authentication/notes/Authentication.txt +5 -0
- data/webgui/vendor/plugins/restful_authentication/notes/Authorization.txt +154 -0
- data/webgui/vendor/plugins/restful_authentication/notes/RailsPlugins.txt +78 -0
- data/webgui/vendor/plugins/restful_authentication/notes/SecurityFramework.graffle +0 -0
- data/webgui/vendor/plugins/restful_authentication/notes/SecurityFramework.png +0 -0
- data/webgui/vendor/plugins/restful_authentication/notes/SecurityPatterns.txt +163 -0
- data/webgui/vendor/plugins/restful_authentication/notes/Tradeoffs.txt +126 -0
- data/webgui/vendor/plugins/restful_authentication/notes/Trustification.txt +49 -0
- data/webgui/vendor/plugins/restful_authentication/rails/init.rb +3 -0
- data/webgui/vendor/plugins/restful_authentication/restful-authentication.gemspec +33 -0
- data/webgui/vendor/plugins/restful_authentication/tasks/auth.rake +33 -0
- metadata +288 -8
- data/tasks/annotations.rake +0 -22
@@ -0,0 +1,109 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class UserTest < ActiveSupport::TestCase
|
4
|
+
# Be sure to include AuthenticatedTestHelper in test/test_helper.rb instead.
|
5
|
+
# Then, you can remove it from this and the functional test.
|
6
|
+
include AuthenticatedTestHelper
|
7
|
+
fixtures :users
|
8
|
+
|
9
|
+
def test_should_create_user
|
10
|
+
assert_difference 'User.count' do
|
11
|
+
user = create_user
|
12
|
+
assert !user.new_record?, "#{user.errors.full_messages.to_sentence}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_initialize_activation_code_upon_creation
|
17
|
+
user = create_user
|
18
|
+
user.reload
|
19
|
+
assert_not_nil user.activation_code
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_should_require_login
|
23
|
+
assert_no_difference 'User.count' do
|
24
|
+
u = create_user(:login => nil)
|
25
|
+
assert u.errors.on(:login)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_require_password
|
30
|
+
assert_no_difference 'User.count' do
|
31
|
+
u = create_user(:password => nil)
|
32
|
+
assert u.errors.on(:password)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_should_require_password_confirmation
|
37
|
+
assert_no_difference 'User.count' do
|
38
|
+
u = create_user(:password_confirmation => nil)
|
39
|
+
assert u.errors.on(:password_confirmation)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_should_require_email
|
44
|
+
assert_no_difference 'User.count' do
|
45
|
+
u = create_user(:email => nil)
|
46
|
+
assert u.errors.on(:email)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_should_reset_password
|
51
|
+
users(:quentin).update_attributes(:password => 'new password', :password_confirmation => 'new password')
|
52
|
+
assert_equal users(:quentin), User.authenticate('quentin', 'new password')
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_should_not_rehash_password
|
56
|
+
users(:quentin).update_attributes(:login => 'quentin2')
|
57
|
+
assert_equal users(:quentin), User.authenticate('quentin2', 'monkey')
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_should_authenticate_user
|
61
|
+
assert_equal users(:quentin), User.authenticate('quentin', 'monkey')
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_should_set_remember_token
|
65
|
+
users(:quentin).remember_me
|
66
|
+
assert_not_nil users(:quentin).remember_token
|
67
|
+
assert_not_nil users(:quentin).remember_token_expires_at
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_should_unset_remember_token
|
71
|
+
users(:quentin).remember_me
|
72
|
+
assert_not_nil users(:quentin).remember_token
|
73
|
+
users(:quentin).forget_me
|
74
|
+
assert_nil users(:quentin).remember_token
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_should_remember_me_for_one_week
|
78
|
+
before = 1.week.from_now.utc
|
79
|
+
users(:quentin).remember_me_for 1.week
|
80
|
+
after = 1.week.from_now.utc
|
81
|
+
assert_not_nil users(:quentin).remember_token
|
82
|
+
assert_not_nil users(:quentin).remember_token_expires_at
|
83
|
+
assert users(:quentin).remember_token_expires_at.between?(before, after)
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_should_remember_me_until_one_week
|
87
|
+
time = 1.week.from_now.utc
|
88
|
+
users(:quentin).remember_me_until time
|
89
|
+
assert_not_nil users(:quentin).remember_token
|
90
|
+
assert_not_nil users(:quentin).remember_token_expires_at
|
91
|
+
assert_equal users(:quentin).remember_token_expires_at, time
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_should_remember_me_default_two_weeks
|
95
|
+
before = 2.weeks.from_now.utc
|
96
|
+
users(:quentin).remember_me
|
97
|
+
after = 2.weeks.from_now.utc
|
98
|
+
assert_not_nil users(:quentin).remember_token
|
99
|
+
assert_not_nil users(:quentin).remember_token_expires_at
|
100
|
+
assert users(:quentin).remember_token_expires_at.between?(before, after)
|
101
|
+
end
|
102
|
+
|
103
|
+
protected
|
104
|
+
def create_user(options = {})
|
105
|
+
record = User.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire69', :password_confirmation => 'quire69' }.merge(options))
|
106
|
+
record.save
|
107
|
+
record
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Example:
|
2
|
+
|
3
|
+
# Controller
|
4
|
+
class BlogController < ApplicationController
|
5
|
+
auto_complete_for :post, :title
|
6
|
+
end
|
7
|
+
|
8
|
+
# View
|
9
|
+
<%= text_field_with_auto_complete :post, title %>
|
10
|
+
|
11
|
+
By default, auto_complete_for limits the results to 10 entries,
|
12
|
+
and sorts by the given field.
|
13
|
+
|
14
|
+
auto_complete_for takes a third parameter, an options hash to
|
15
|
+
the find method used to search for the records:
|
16
|
+
|
17
|
+
auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
|
18
|
+
|
19
|
+
For more examples, see script.aculo.us:
|
20
|
+
* http://script.aculo.us/demos/ajax/autocompleter
|
21
|
+
* http://script.aculo.us/demos/ajax/autocompleter_customized
|
22
|
+
|
23
|
+
Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license
|
@@ -0,0 +1,22 @@
|
|
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 auto_complete plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for auto_complete plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Auto Complete'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module AutoComplete
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# # Controller
|
11
|
+
# class BlogController < ApplicationController
|
12
|
+
# auto_complete_for :post, :title
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # View
|
16
|
+
# <%= text_field_with_auto_complete :post, title %>
|
17
|
+
#
|
18
|
+
# By default, auto_complete_for limits the results to 10 entries,
|
19
|
+
# and sorts by the given field.
|
20
|
+
#
|
21
|
+
# auto_complete_for takes a third parameter, an options hash to
|
22
|
+
# the find method used to search for the records:
|
23
|
+
#
|
24
|
+
# auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
|
25
|
+
#
|
26
|
+
# For help on defining text input fields with autocompletion,
|
27
|
+
# see ActionView::Helpers::JavaScriptHelper.
|
28
|
+
#
|
29
|
+
# For more examples, see script.aculo.us:
|
30
|
+
# * http://script.aculo.us/demos/ajax/autocompleter
|
31
|
+
# * http://script.aculo.us/demos/ajax/autocompleter_customized
|
32
|
+
module ClassMethods
|
33
|
+
def auto_complete_for(object, method, options = {})
|
34
|
+
define_method("auto_complete_for_#{object}_#{method}") do
|
35
|
+
find_options = {
|
36
|
+
:conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ],
|
37
|
+
:order => "#{method} ASC",
|
38
|
+
:limit => 10 }.merge!(options)
|
39
|
+
|
40
|
+
@items = object.to_s.camelize.constantize.find(:all, find_options)
|
41
|
+
|
42
|
+
render :inline => "<%= auto_complete_result @items, '#{method}' %>"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
module AutoCompleteMacrosHelper
|
2
|
+
# Adds AJAX autocomplete functionality to the text input field with the
|
3
|
+
# DOM ID specified by +field_id+.
|
4
|
+
#
|
5
|
+
# This function expects that the called action returns an HTML <ul> list,
|
6
|
+
# or nothing if no entries should be displayed for autocompletion.
|
7
|
+
#
|
8
|
+
# You'll probably want to turn the browser's built-in autocompletion off,
|
9
|
+
# so be sure to include an <tt>autocomplete="off"</tt> attribute with your text
|
10
|
+
# input field.
|
11
|
+
#
|
12
|
+
# The autocompleter object is assigned to a Javascript variable named <tt>field_id</tt>_auto_completer.
|
13
|
+
# This object is useful if you for example want to trigger the auto-complete suggestions through
|
14
|
+
# other means than user input (for that specific case, call the <tt>activate</tt> method on that object).
|
15
|
+
#
|
16
|
+
# Required +options+ are:
|
17
|
+
# <tt>:url</tt>:: URL to call for autocompletion results
|
18
|
+
# in url_for format.
|
19
|
+
#
|
20
|
+
# Addtional +options+ are:
|
21
|
+
# <tt>:update</tt>:: Specifies the DOM ID of the element whose
|
22
|
+
# innerHTML should be updated with the autocomplete
|
23
|
+
# entries returned by the AJAX request.
|
24
|
+
# Defaults to <tt>field_id</tt> + '_auto_complete'
|
25
|
+
# <tt>:with</tt>:: A JavaScript expression specifying the
|
26
|
+
# parameters for the XMLHttpRequest. This defaults
|
27
|
+
# to 'fieldname=value'.
|
28
|
+
# <tt>:frequency</tt>:: Determines the time to wait after the last keystroke
|
29
|
+
# for the AJAX request to be initiated.
|
30
|
+
# <tt>:indicator</tt>:: Specifies the DOM ID of an element which will be
|
31
|
+
# displayed while autocomplete is running.
|
32
|
+
# <tt>:tokens</tt>:: A string or an array of strings containing
|
33
|
+
# separator tokens for tokenized incremental
|
34
|
+
# autocompletion. Example: <tt>:tokens => ','</tt> would
|
35
|
+
# allow multiple autocompletion entries, separated
|
36
|
+
# by commas.
|
37
|
+
# <tt>:min_chars</tt>:: The minimum number of characters that should be
|
38
|
+
# in the input field before an Ajax call is made
|
39
|
+
# to the server.
|
40
|
+
# <tt>:on_hide</tt>:: A Javascript expression that is called when the
|
41
|
+
# autocompletion div is hidden. The expression
|
42
|
+
# should take two variables: element and update.
|
43
|
+
# Element is a DOM element for the field, update
|
44
|
+
# is a DOM element for the div from which the
|
45
|
+
# innerHTML is replaced.
|
46
|
+
# <tt>:on_show</tt>:: Like on_hide, only now the expression is called
|
47
|
+
# then the div is shown.
|
48
|
+
# <tt>:after_update_element</tt>:: A Javascript expression that is called when the
|
49
|
+
# user has selected one of the proposed values.
|
50
|
+
# The expression should take two variables: element and value.
|
51
|
+
# Element is a DOM element for the field, value
|
52
|
+
# is the value selected by the user.
|
53
|
+
# <tt>:select</tt>:: Pick the class of the element from which the value for
|
54
|
+
# insertion should be extracted. If this is not specified,
|
55
|
+
# the entire element is used.
|
56
|
+
# <tt>:method</tt>:: Specifies the HTTP verb to use when the autocompletion
|
57
|
+
# request is made. Defaults to POST.
|
58
|
+
def auto_complete_field(field_id, options = {})
|
59
|
+
function = "var #{field_id}_auto_completer = new Ajax.Autocompleter("
|
60
|
+
function << "'#{field_id}', "
|
61
|
+
function << "'" + (options[:update] || "#{field_id}_auto_complete") + "', "
|
62
|
+
function << "'#{url_for(options[:url])}'"
|
63
|
+
|
64
|
+
js_options = {}
|
65
|
+
js_options[:tokens] = array_or_string_for_javascript(options[:tokens]) if options[:tokens]
|
66
|
+
js_options[:callback] = "function(element, value) { return #{options[:with]} }" if options[:with]
|
67
|
+
js_options[:indicator] = "'#{options[:indicator]}'" if options[:indicator]
|
68
|
+
js_options[:select] = "'#{options[:select]}'" if options[:select]
|
69
|
+
js_options[:paramName] = "'#{options[:param_name]}'" if options[:param_name]
|
70
|
+
js_options[:frequency] = "#{options[:frequency]}" if options[:frequency]
|
71
|
+
js_options[:method] = "'#{options[:method].to_s}'" if options[:method]
|
72
|
+
|
73
|
+
{ :after_update_element => :afterUpdateElement,
|
74
|
+
:on_show => :onShow, :on_hide => :onHide, :min_chars => :minChars }.each do |k,v|
|
75
|
+
js_options[v] = options[k] if options[k]
|
76
|
+
end
|
77
|
+
|
78
|
+
function << (', ' + options_for_javascript(js_options) + ')')
|
79
|
+
|
80
|
+
javascript_tag(function)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Use this method in your view to generate a return for the AJAX autocomplete requests.
|
84
|
+
#
|
85
|
+
# Example action:
|
86
|
+
#
|
87
|
+
# def auto_complete_for_item_title
|
88
|
+
# @items = Item.find(:all,
|
89
|
+
# :conditions => [ 'LOWER(description) LIKE ?',
|
90
|
+
# '%' + request.raw_post.downcase + '%' ])
|
91
|
+
# render :inline => "<%= auto_complete_result(@items, 'description') %>"
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# The auto_complete_result can of course also be called from a view belonging to the
|
95
|
+
# auto_complete action if you need to decorate it further.
|
96
|
+
def auto_complete_result(entries, field, phrase = nil)
|
97
|
+
return unless entries
|
98
|
+
items = entries.map { |entry| content_tag("li", phrase ? highlight(entry[field], phrase) : h(entry[field])) }
|
99
|
+
content_tag("ul", items.uniq)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Wrapper for text_field with added AJAX autocompletion functionality.
|
103
|
+
#
|
104
|
+
# In your controller, you'll need to define an action called
|
105
|
+
# auto_complete_for to respond the AJAX calls,
|
106
|
+
#
|
107
|
+
def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
|
108
|
+
(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
|
109
|
+
text_field(object, method, tag_options) +
|
110
|
+
content_tag("div", "", :id => "#{object}_#{method}_auto_complete", :class => "auto_complete") +
|
111
|
+
auto_complete_field("#{object}_#{method}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
def auto_complete_stylesheet
|
116
|
+
content_tag('style', <<-EOT, :type => Mime::CSS)
|
117
|
+
div.auto_complete {
|
118
|
+
width: 350px;
|
119
|
+
background: #fff;
|
120
|
+
}
|
121
|
+
div.auto_complete ul {
|
122
|
+
border:1px solid #888;
|
123
|
+
margin:0;
|
124
|
+
padding:0;
|
125
|
+
width:100%;
|
126
|
+
list-style-type:none;
|
127
|
+
}
|
128
|
+
div.auto_complete ul li {
|
129
|
+
margin:0;
|
130
|
+
padding:3px;
|
131
|
+
}
|
132
|
+
div.auto_complete ul li.selected {
|
133
|
+
background-color: #ffb;
|
134
|
+
}
|
135
|
+
div.auto_complete ul strong.highlight {
|
136
|
+
color: #800;
|
137
|
+
margin:0;
|
138
|
+
padding:0;
|
139
|
+
}
|
140
|
+
EOT
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../test/test_helper'))
|
2
|
+
|
3
|
+
class AutoCompleteTest < Test::Unit::TestCase
|
4
|
+
include AutoComplete
|
5
|
+
include AutoCompleteMacrosHelper
|
6
|
+
|
7
|
+
include ActionView::Helpers::UrlHelper
|
8
|
+
include ActionView::Helpers::TagHelper
|
9
|
+
include ActionView::Helpers::TextHelper
|
10
|
+
include ActionView::Helpers::FormHelper
|
11
|
+
include ActionView::Helpers::CaptureHelper
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@controller = Class.new do
|
15
|
+
def url_for(options)
|
16
|
+
url = "http://www.example.com/"
|
17
|
+
url << options[:action].to_s if options and options[:action]
|
18
|
+
url
|
19
|
+
end
|
20
|
+
end
|
21
|
+
@controller = @controller.new
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
def test_auto_complete_field
|
26
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {})\n//]]>\n</script>),
|
27
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" });
|
28
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {tokens:','})\n//]]>\n</script>),
|
29
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :tokens => ',');
|
30
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {tokens:[',']})\n//]]>\n</script>),
|
31
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :tokens => [',']);
|
32
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {minChars:3})\n//]]>\n</script>),
|
33
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :min_chars => 3);
|
34
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {onHide:function(element, update){alert('me');}})\n//]]>\n</script>),
|
35
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :on_hide => "function(element, update){alert('me');}");
|
36
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {frequency:2})\n//]]>\n</script>),
|
37
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :frequency => 2);
|
38
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {afterUpdateElement:function(element,value){alert('You have chosen: '+value)}})\n//]]>\n</script>),
|
39
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" },
|
40
|
+
:after_update_element => "function(element,value){alert('You have chosen: '+value)}");
|
41
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {paramName:'huidriwusch'})\n//]]>\n</script>),
|
42
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :param_name => 'huidriwusch');
|
43
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {method:'get'})\n//]]>\n</script>),
|
44
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :method => :get);
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_auto_complete_result
|
48
|
+
result = [ { :title => 'test1' }, { :title => 'test2' } ]
|
49
|
+
assert_equal %(<ul><li>test1</li><li>test2</li></ul>),
|
50
|
+
auto_complete_result(result, :title)
|
51
|
+
assert_equal %(<ul><li>t<strong class=\"highlight\">est</strong>1</li><li>t<strong class=\"highlight\">est</strong>2</li></ul>),
|
52
|
+
auto_complete_result(result, :title, "est")
|
53
|
+
|
54
|
+
resultuniq = [ { :title => 'test1' }, { :title => 'test1' } ]
|
55
|
+
assert_equal %(<ul><li>t<strong class=\"highlight\">est</strong>1</li></ul>),
|
56
|
+
auto_complete_result(resultuniq, :title, "est")
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_text_field_with_auto_complete
|
60
|
+
assert_match %(<style type="text/css">),
|
61
|
+
text_field_with_auto_complete(:message, :recipient)
|
62
|
+
|
63
|
+
assert_dom_equal %(<input id=\"message_recipient\" name=\"message[recipient]\" size=\"30\" type=\"text\" /><div class=\"auto_complete\" id=\"message_recipient_auto_complete\"></div><script type=\"text/javascript\">\n//<![CDATA[\nvar message_recipient_auto_completer = new Ajax.Autocompleter('message_recipient', 'message_recipient_auto_complete', 'http://www.example.com/auto_complete_for_message_recipient', {})\n//]]>\n</script>),
|
64
|
+
text_field_with_auto_complete(:message, :recipient, {}, :skip_style => true)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
I have moved the enumerations_mixin plugin to a
|
2
|
+
new location at:
|
3
|
+
|
4
|
+
http://svn.protocool.com/public/plugins/enumerations_mixin/
|
5
|
+
|
6
|
+
So, if you're looking at this file from a web browser
|
7
|
+
and wondering why your svn:externals suddenly stopped working,
|
8
|
+
that's why.
|
9
|
+
|
10
|
+
While you're updating your svn:externals you might want
|
11
|
+
to take a look at Piston - it's a much better way to
|
12
|
+
manage your plugins and externals:
|
13
|
+
|
14
|
+
http://piston.rubyforge.org/
|
15
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2005 Trevor Squires
|
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
|
9
|
+
to 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 NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
18
|
+
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
19
|
+
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,163 @@
|
|
1
|
+
Enumerations Mixin
|
2
|
+
Copyright (c) 2005 Trevor Squires
|
3
|
+
Released under the MIT License. See the LICENSE file for more details.
|
4
|
+
|
5
|
+
What is this?:
|
6
|
+
|
7
|
+
The enumerations mixin allows you to treat instances of your
|
8
|
+
ActiveRecord models as though they were an enumeration of values.
|
9
|
+
|
10
|
+
At it's most basic level, it allows you to say things along the lines of:
|
11
|
+
|
12
|
+
booking = Booking.new(:status => BookingStatus[:provisional])
|
13
|
+
booking.update_attribute(:status, BookingStatus[:confirmed])
|
14
|
+
|
15
|
+
Booking.find :first,
|
16
|
+
:conditions => ['status_id = ?', BookingStatus[:provisional].id]
|
17
|
+
|
18
|
+
BookingStatus.all.collect {|status|, [status.name, status.id]}
|
19
|
+
|
20
|
+
See "How to use it" below for more information.
|
21
|
+
|
22
|
+
Package Contents:
|
23
|
+
|
24
|
+
This package adds two mixins and a helper to Rails' ActiveRecord:
|
25
|
+
|
26
|
+
acts_as_enumerated provides capabilities to treat your model and
|
27
|
+
its records as an enumeration. At a minimum, the database table for
|
28
|
+
an acts_as_enumerated must contain an 'id' column and a 'name'
|
29
|
+
column. All instances for the acts_as_enumerated model are cached
|
30
|
+
in memory.
|
31
|
+
|
32
|
+
has_enumerated adds methods to your ActiveRecord model for setting
|
33
|
+
and retrieving enumerated values using an associated acts_as_enumerated
|
34
|
+
model.
|
35
|
+
|
36
|
+
There is also an ActiveRecord::VirtualEnumerations helper module
|
37
|
+
to create 'virtual' acts_as_enumerated models which helps to avoid
|
38
|
+
cluttering up your models directory with acts_as_enumerated classes.
|
39
|
+
|
40
|
+
How to use it:
|
41
|
+
|
42
|
+
* acts_as_enumerated
|
43
|
+
|
44
|
+
class BookingStatus < ActiveRecord::Base
|
45
|
+
acts_as_enumerated :conditions => 'optional_sql_conditions',
|
46
|
+
:order => 'optional_sql_orderby',
|
47
|
+
:on_lookup_failure => :optional_class_method
|
48
|
+
end
|
49
|
+
|
50
|
+
With that, your BookingStatus class will have the following methods defined:
|
51
|
+
|
52
|
+
* BookingStatus[arg] : lookup the BookingStatus instance for arg.
|
53
|
+
The arg value can be a 'string' or a :symbol, in which case the
|
54
|
+
lookup will be against the BookingStatus.name field. Alternatively
|
55
|
+
arg can be a Fixnum, in which case the lookup will be against the
|
56
|
+
BookingStatus.id field.
|
57
|
+
|
58
|
+
The :on_lookup_failure option specifies the name of a class method
|
59
|
+
to invoke when the [] method is unable to locate a BookingStatus
|
60
|
+
record for arg. The defeault is the built-in :enforce_strict_literals
|
61
|
+
which causes an exception to be raised when no record is found and
|
62
|
+
the arg is a Fixnum or Symbol, otherwise it returns nil. There are
|
63
|
+
also built-ins for :enforce_strict (raise and exception regardless
|
64
|
+
of the type for arg) and :enforce_none which just returns nil.
|
65
|
+
|
66
|
+
The whole point of the :on_lookup_failure option is that I'm pretty
|
67
|
+
opinionated that a) if the value can't be looked-up for a :symbol
|
68
|
+
that I've passed, it's probably a typo so I want a *big* hint that
|
69
|
+
something is wrong and b) it's likely that my opinion isn't shared
|
70
|
+
by everyone so it should be configurable.
|
71
|
+
|
72
|
+
* BookingStatus.all : returns an array of all BookingStatus records
|
73
|
+
that match the :conditions specified in acts_as_enumerated, in the
|
74
|
+
order specified by :order.
|
75
|
+
|
76
|
+
NOTE: acts_as_enumerated records are considered immutable. By
|
77
|
+
default you cannot create/alter/destroy instances because they are
|
78
|
+
cached in memory. Because of Rails' process-based model it is not
|
79
|
+
safe to allow updating acts_as_enumerated records as the caches
|
80
|
+
will get out of sync.
|
81
|
+
|
82
|
+
However, one instance where updating the models *should* be allowed
|
83
|
+
is if you are using ActiveRecord Migrations.
|
84
|
+
|
85
|
+
Using the above example you would do the following:
|
86
|
+
|
87
|
+
BookingStatus.enumeration_model_updates_permitted = true
|
88
|
+
BookingStatus.create(:name => 'newname')
|
89
|
+
|
90
|
+
* has_enumerated
|
91
|
+
|
92
|
+
First of all, note that you *could* specify the relationship
|
93
|
+
to an acts_as_enumerated class using the belongs_to association.
|
94
|
+
However, has_enumerated is preferable because you aren't really
|
95
|
+
associated to the enumerated value, you are *aggregating* it.
|
96
|
+
As such, the has_enumerated macro behaves more like an aggregation
|
97
|
+
than an association.
|
98
|
+
|
99
|
+
class Booking < ActiveRecord::Base
|
100
|
+
has_enumerated :status, :class_name => 'BookingStatus',
|
101
|
+
:foreign_key => 'status_id',
|
102
|
+
:on_lookup_failure => :optional_instance_method
|
103
|
+
end
|
104
|
+
|
105
|
+
By default, the foreign key is interpreted to be the name of your
|
106
|
+
has_enumerated field (in this case 'status') plus '_id'. Additionally,
|
107
|
+
the default value for :class_name is the camel-ized version of the
|
108
|
+
name for your has_enumerated field. :on_lookup_failure is explained
|
109
|
+
below.
|
110
|
+
|
111
|
+
With that, your Booking class will have the following methods defined:
|
112
|
+
|
113
|
+
* status : returns the BookingStatus with an id that matches the
|
114
|
+
value in the Booking.status_id.
|
115
|
+
|
116
|
+
* status= : sets the value for Booking.status_id using the id of
|
117
|
+
the BookingStatus instance passed as an argument. As a short-hand,
|
118
|
+
you can also pass it the 'name' of a BookingStatus instance, either
|
119
|
+
as a 'string' or :symbol.
|
120
|
+
|
121
|
+
I.e. mybooking.status = :confirmed or mybooking.update_attribute(:status,
|
122
|
+
:confirmed)
|
123
|
+
|
124
|
+
The :on_lookup_failure option in has_enumerated is there because
|
125
|
+
(again) I'm opinionated about what should happen. By default, if
|
126
|
+
booking.status_id contains an id that isn't a valid BookingStatus.id
|
127
|
+
I just want booking.status to return nil rather than throw an
|
128
|
+
exception. This ensures I can edit my Booking instances without
|
129
|
+
having to put rescue blocks around all my booking.status calls.
|
130
|
+
However, if I call booking.status = :bogus I want noisy hints about
|
131
|
+
the bug.
|
132
|
+
|
133
|
+
Of course, you may not agree with that in which case you can specify
|
134
|
+
an *instance* method to be called in the case of a lookup failure.
|
135
|
+
The method signature is as follows:
|
136
|
+
|
137
|
+
your_lookup_handler(operation, name, name_foreign_key,
|
138
|
+
acts_enumerated_class_name, lookup_value)
|
139
|
+
|
140
|
+
The 'operation' arg will be either :read or :write. In the case
|
141
|
+
of :read you are expected to return something or raise an exception,
|
142
|
+
while in the case of a :write you don't have to return anything.
|
143
|
+
|
144
|
+
Note that there's enough information in the method signature that
|
145
|
+
you can specify one method to handle all lookup failures for all
|
146
|
+
has_enumerated fields if you happen to have more than one defined
|
147
|
+
in your model.
|
148
|
+
|
149
|
+
* ActiveRecord::VirtualEnumerations
|
150
|
+
|
151
|
+
For the most part, your acts_as_enumerated classes will do nothing
|
152
|
+
more than just act as enumerated.
|
153
|
+
|
154
|
+
In that case there isn't much point cluttering up your models
|
155
|
+
directory with those class files. You can use
|
156
|
+
ActiveRecord::VirtualEnumerations to reduce that clutter.
|
157
|
+
|
158
|
+
Copy virtual_enumerations_sample.rb to
|
159
|
+
RAILS_ROOT/config/virtual_enumerations.rb and configure it accordingly.
|
160
|
+
|
161
|
+
See virtual_enumerations_sample.rb in this directory for a full
|
162
|
+
description.
|
163
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Copyright (c) 2005 Trevor Squires
|
2
|
+
# Released under the MIT License. See the LICENSE file for more details.
|
3
|
+
|
4
|
+
require 'active_record/acts/enumerated'
|
5
|
+
require 'active_record/aggregations/has_enumerated'
|
6
|
+
ActiveRecord::Base.class_eval do
|
7
|
+
include ActiveRecord::Acts::Enumerated
|
8
|
+
include ActiveRecord::Aggregations::HasEnumerated
|
9
|
+
end
|
10
|
+
|
11
|
+
# Virtual enumerations are useful if you've got a ton of different
|
12
|
+
# enumerations and don't care to litter your models directory with them.
|
13
|
+
# It's also handy if you want to define singleton methods for your
|
14
|
+
# enumerated values.
|
15
|
+
# See virtual_sample.txt in this directory for more info
|
16
|
+
if File.exist?("#{RAILS_ROOT}/config/virtual_enumerations.rb")
|
17
|
+
require 'active_record/virtual_enumerations'
|
18
|
+
silence_warnings do
|
19
|
+
eval(IO.read("#{RAILS_ROOT}/config/virtual_enumerations.rb"), binding)
|
20
|
+
end
|
21
|
+
end
|