hobo 1.1.0 → 1.3.0.RC
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.txt +238 -287
- data/Rakefile +14 -31
- data/VERSION +1 -0
- data/{lib/hobo → app/controllers}/dev_controller.rb +6 -6
- data/app/controllers/dryml_support_controller.rb +13 -0
- data/{rails_generators/hobo_front_controller/templates → app/views/dev}/summary.dryml +7 -10
- data/bin/hobo +12 -144
- data/config/initializers/inflections.rb +3 -0
- data/config/routes.rb +6 -0
- data/{doctest → doctests}/hobo/lifecycles.rdoctest +40 -59
- data/{doctest → doctests/hobo}/model.rdoctest +33 -40
- data/{doctest → doctests/hobo}/multi_model_forms.rdoctest +15 -19
- data/{doctest → doctests/hobo}/scopes.rdoctest +27 -41
- data/doctests/prepare_testapp.rb +8 -0
- data/hobo.gemspec +26 -199
- data/lib/generators/hobo/activation_email.rb +11 -0
- data/lib/generators/hobo/admin_subsite/admin_subsite_generator.rb +35 -0
- data/lib/generators/hobo/admin_subsite/templates/admin.css +20 -0
- data/lib/generators/hobo/admin_subsite/templates/admin_tag_injection.erb +10 -0
- data/{rails_generators/hobo_admin_site → lib/generators/hobo/admin_subsite}/templates/application.dryml +0 -0
- data/{rails_generators/hobo_admin_site/templates/controller.rb → lib/generators/hobo/admin_subsite/templates/controller.rb.erb} +5 -5
- data/lib/generators/hobo/admin_subsite/templates/users_index.dryml +7 -0
- data/lib/generators/hobo/assets/assets_generator.rb +18 -0
- data/{rails_generators/hobo → lib/generators/hobo/assets}/templates/application.css +0 -0
- data/lib/generators/hobo/assets/templates/application.dryml.erb +9 -0
- data/{rails_generators/hobo → lib/generators/hobo/assets}/templates/dryml-support.js +2 -2
- data/lib/generators/hobo/assets/templates/dryml_taglibs_initializer.rb +1 -0
- data/lib/generators/hobo/assets/templates/en_injection.yml +19 -0
- data/{rails_generators/hobo → lib/generators/hobo/assets}/templates/guest.rb +1 -1
- data/lib/generators/hobo/controller.rb +35 -0
- data/lib/generators/hobo/controller/controller_generator.rb +6 -0
- data/lib/generators/hobo/controller/templates/controller.rb.erb +7 -0
- data/{rails_generators/hobo_front_controller → lib/generators/hobo/front_controller}/USAGE +0 -0
- data/lib/generators/hobo/front_controller/front_controller_generator.rb +55 -0
- data/{rails_generators/hobo_front_controller/templates/controller.rb → lib/generators/hobo/front_controller/templates/controller.rb.erb} +0 -0
- data/{rails_generators/hobo_front_controller → lib/generators/hobo/front_controller}/templates/index.dryml +6 -12
- data/lib/generators/hobo/i18n/i18n_generator.rb +35 -0
- data/lib/generators/hobo/i18n/templates/app.de.yml +30 -0
- data/lib/generators/hobo/i18n/templates/app.en.yml +25 -0
- data/lib/generators/hobo/i18n/templates/app.es.yml +31 -0
- data/lib/generators/hobo/i18n/templates/app.fr.yml +26 -0
- data/lib/generators/hobo/i18n/templates/app.it.yml +28 -0
- data/lib/generators/hobo/i18n/templates/app.pt-PT.yml +25 -0
- data/lib/generators/hobo/i18n/templates/app.ru.yml +24 -0
- data/lib/generators/hobo/i18n/templates/hobo.de.yml +204 -0
- data/lib/generators/hobo/i18n/templates/hobo.en.yml +195 -0
- data/lib/generators/hobo/i18n/templates/hobo.es.yml +202 -0
- data/lib/generators/hobo/i18n/templates/hobo.fr.yml +195 -0
- data/lib/generators/hobo/i18n/templates/hobo.it.yml +202 -0
- data/lib/generators/hobo/i18n/templates/hobo.pt-PT.yml +196 -0
- data/lib/generators/hobo/i18n/templates/hobo.ru.yml +200 -0
- data/lib/generators/hobo/invite_only.rb +18 -0
- data/lib/generators/hobo/model/USAGE +19 -0
- data/lib/generators/hobo/model/model_generator.rb +11 -0
- data/{rails_generators/hobo_model/templates/model.rb → lib/generators/hobo/model/templates/model_injection.rb.erb} +0 -2
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/USAGE +0 -0
- data/lib/generators/hobo/rapid/rapid_generator.rb +24 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/IE7.js +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/blank.gif +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/hobo-rapid.css +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/hobo-rapid.js +65 -65
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/ie7-recalc.js +21 -21
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/lowpro.js +31 -31
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/reset.css +1 -1
- data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/public/images/100-ACD3E6-DBE1E5-H.png +0 -0
- data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/public/images/100-DBE1E5-FCFEF5-H.png +0 -0
- data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/public/images/300-3B5F87-ACD3E6-H.png +0 -0
- data/{rails_generators/hobo_rapid/templates/themes/clean → lib/generators/hobo/rapid/templates/themes/clean-sidemenu}/public/images/spinner.gif +0 -0
- data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/public/stylesheets/clean-sidemenu.css +81 -0
- data/lib/generators/hobo/rapid/templates/themes/clean-sidemenu/views/clean-sidemenu.dryml +30 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/101-3B5F87-ACD3E6.png +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/30-3E547A-242E42.png +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/30-DBE1E5-FCFEF5.png +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/300-ACD3E6-fff.png +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/50-ACD3E6-fff.png +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/fieldbg.gif +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/pencil.png +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/images/small_close.png +0 -0
- data/lib/generators/hobo/rapid/templates/themes/clean/public/images/spinner.gif +0 -0
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/stylesheets/clean.css +16 -16
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/public/stylesheets/rapid-ui.css +3 -3
- data/{rails_generators/hobo_rapid → lib/generators/hobo/rapid}/templates/themes/clean/views/clean.dryml +1 -1
- data/lib/generators/hobo/resource/resource_generator.rb +23 -0
- data/lib/generators/hobo/routes/USAGE +9 -0
- data/lib/generators/hobo/routes/router.rb +115 -0
- data/lib/generators/hobo/routes/routes_generator.rb +40 -0
- data/lib/generators/hobo/routes/templates/hobo_routes.rb.erb +33 -0
- data/lib/generators/hobo/setup_wizard/USAGE +4 -0
- data/lib/generators/hobo/setup_wizard/setup_wizard_generator.rb +274 -0
- data/lib/generators/hobo/subsite.rb +48 -0
- data/lib/generators/hobo/subsite/subsite_generator.rb +16 -0
- data/{rails_generators/hobo_subsite → lib/generators/hobo/subsite}/templates/application.dryml +0 -0
- data/{rails_generators/hobo_subsite/templates/controller.rb → lib/generators/hobo/subsite/templates/controller.rb.erb} +1 -1
- data/lib/generators/hobo/subsite_taglib/subsite_taglib_generator.rb +16 -0
- data/{rails_generators/hobo_admin_site/templates/site_taglib.dryml → lib/generators/hobo/subsite_taglib/templates/taglib.dryml.erb} +9 -10
- data/lib/generators/hobo/taglib.rb +12 -0
- data/lib/generators/hobo/test_framework/test_framework_generator.rb +72 -0
- data/lib/generators/hobo/test_options.rb +19 -0
- data/{rails_generators/hobo_user_controller → lib/generators/hobo/user_controller}/templates/accept_invitation.dryml +0 -0
- data/lib/generators/hobo/user_controller/templates/controller.rb.erb +31 -0
- data/lib/generators/hobo/user_controller/user_controller_generator.rb +25 -0
- data/lib/generators/hobo/user_mailer/templates/activation.erb +9 -0
- data/{rails_generators/hobo_user_model → lib/generators/hobo/user_mailer}/templates/forgot_password.erb +2 -2
- data/{rails_generators/hobo_user_model → lib/generators/hobo/user_mailer}/templates/invite.erb +2 -2
- data/lib/generators/hobo/user_mailer/templates/mailer.rb.erb +25 -0
- data/lib/generators/hobo/user_mailer/user_mailer_generator.rb +33 -0
- data/lib/generators/hobo/user_model/USAGE +12 -0
- data/{rails_generators/hobo_user_model/templates/model.rb → lib/generators/hobo/user_model/templates/model_injection.rb.erb} +40 -20
- data/lib/generators/hobo/user_model/user_model_generator.rb +23 -0
- data/lib/generators/hobo/user_resource/user_resource_generator.rb +27 -0
- data/lib/hobo.rb +29 -129
- data/lib/hobo/controller.rb +37 -47
- data/lib/hobo/controller/authentication_support.rb +109 -0
- data/lib/hobo/{model_controller.rb → controller/model.rb} +71 -79
- data/lib/hobo/{user_controller.rb → controller/user.rb} +59 -49
- data/lib/hobo/engine.rb +80 -0
- data/lib/hobo/extensions/action_controller/hobo_methods.rb +44 -0
- data/lib/hobo/extensions/action_mailer/helper.rb +38 -0
- data/lib/{action_view_extensions/helpers → hobo/extensions/action_view}/tag_helper.rb +5 -2
- data/lib/hobo/extensions/action_view/translation_helper.rb +25 -0
- data/lib/hobo/extensions/active_model/name.rb +16 -0
- data/lib/hobo/extensions/active_model/translation.rb +35 -0
- data/lib/{active_record/association_collection.rb → hobo/extensions/active_record/associations/collection.rb} +8 -17
- data/lib/{active_record/association_proxy.rb → hobo/extensions/active_record/associations/proxy.rb} +6 -7
- data/lib/hobo/extensions/active_record/associations/reflection.rb +23 -0
- data/lib/hobo/extensions/active_record/associations/scope.rb +35 -0
- data/lib/hobo/extensions/active_record/hobo_methods.rb +11 -0
- data/lib/hobo/extensions/active_record/permissions.rb +159 -0
- data/lib/hobo/extensions/active_record/relation_with_origin.rb +28 -0
- data/lib/hobo/extensions/array.rb +27 -0
- data/lib/hobo/extensions/enumerable.rb +12 -0
- data/lib/hobo/extensions/i18n.rb +17 -0
- data/lib/hobo/{hobo_helper.rb → helper.rb} +58 -31
- data/lib/hobo/helper/translations.rb +54 -0
- data/lib/hobo/helper/translations/normalizer.rb +39 -0
- data/lib/hobo/model.rb +61 -95
- data/lib/hobo/model/accessible_associations.rb +178 -0
- data/lib/hobo/{find_for.rb → model/find_for.rb} +17 -17
- data/lib/hobo/model/guest.rb +25 -0
- data/lib/hobo/model/include_in_save.rb +55 -0
- data/lib/hobo/model/lifecycles.rb +119 -0
- data/lib/hobo/model/lifecycles/actions.rb +146 -0
- data/lib/hobo/model/lifecycles/creator.rb +74 -0
- data/lib/hobo/model/lifecycles/lifecycle.rb +243 -0
- data/lib/hobo/model/lifecycles/state.rb +22 -0
- data/lib/hobo/model/lifecycles/transition.rb +70 -0
- data/lib/hobo/model/permissions.rb +448 -0
- data/lib/hobo/model/scopes.rb +38 -0
- data/lib/hobo/model/scopes/apply_scopes.rb +21 -0
- data/lib/hobo/model/scopes/automatic_scopes.rb +428 -0
- data/lib/hobo/model/user_base.rb +184 -0
- data/lib/hobo/model/view_hints.rb +123 -0
- data/{rapid_generators → lib/hobo/rapid/generators}/rapid/cards.dryml.erb +2 -2
- data/{rapid_generators → lib/hobo/rapid/generators}/rapid/forms.dryml.erb +3 -3
- data/{rapid_generators → lib/hobo/rapid/generators}/rapid/pages.dryml.erb +38 -51
- data/lib/hobo/rapid/helper.rb +166 -0
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid.dryml +6 -5
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_core.dryml +286 -118
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_document_tags.dryml +2 -2
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_editing.dryml +45 -45
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_forms.dryml +190 -158
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_generics.dryml +12 -9
- data/lib/hobo/rapid/taglibs/rapid_i18n.dryml +107 -0
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_lifecycles.dryml +7 -7
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_navigation.dryml +15 -15
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_pages.dryml +37 -36
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_plus.dryml +107 -43
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_summary.dryml +28 -57
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_support.dryml +9 -9
- data/{taglibs → lib/hobo/rapid/taglibs}/rapid_user_pages.dryml +41 -40
- data/lib/hobo/routes.rb +31 -0
- data/{doctest → test/doctest}/hobo/hobo_helper.rdoctest +11 -11
- data/test/irt/generators/admin_subsite.irt +27 -0
- data/test/irt/generators/assets.irt +16 -0
- data/test/irt/generators/controller.irt +6 -0
- data/test/irt/generators/front_controller.irt +30 -0
- data/test/irt/generators/helper.rb +31 -0
- data/test/irt/generators/model.irt +28 -0
- data/test/irt/generators/partials/_account_user_model_tests.rb +21 -0
- data/test/irt/generators/partials/_accounts_users_controller_tests.rb +15 -0
- data/test/irt/generators/partials/_default_user_model_tests.rb +21 -0
- data/test/irt/generators/partials/_default_users_controller_tests.rb +16 -0
- data/test/irt/generators/partials/_house_controller_tests.rb +15 -0
- data/test/irt/generators/partials/_house_model_tests.rb +18 -0
- data/test/irt/generators/partials/_subsite_taglib_admin.rb +4 -0
- data/test/irt/generators/partials/_subsite_taglib_admin_invite_only.rb +3 -0
- data/test/irt/generators/partials/_subsite_taglib_noopt.rb +4 -0
- data/test/irt/generators/partials/_subsite_taglib_variables.rb +27 -0
- data/test/irt/generators/partials/_user_mailer_tests.rb +19 -0
- data/test/irt/generators/rapid.irt +29 -0
- data/test/irt/generators/resource.irt +8 -0
- data/test/irt/generators/subsite.irt +52 -0
- data/test/irt/generators/subsite_taglib.irt +23 -0
- data/test/irt/generators/test_framework.irt +62 -0
- data/test/irt/generators/user_controller.irt +10 -0
- data/test/irt/generators/user_mailer.irt +20 -0
- data/test/irt/generators/user_model.irt +10 -0
- data/test/irt/generators/user_resource.irt +14 -0
- data/test/irt/readme.txt +7 -0
- data/test/permissions/models/models.rb +27 -24
- data/test/permissions/test_permissions.rb +104 -104
- metadata +239 -217
- data/lib/active_record/association_reflection.rb +0 -20
- data/lib/active_record/viewhints_validations_interceptor.rb +0 -9
- data/lib/hobo/accessible_associations.rb +0 -183
- data/lib/hobo/authentication_support.rb +0 -131
- data/lib/hobo/generator.rb +0 -26
- data/lib/hobo/guest.rb +0 -25
- data/lib/hobo/include_in_save.rb +0 -55
- data/lib/hobo/lifecycles.rb +0 -137
- data/lib/hobo/lifecycles/actions.rb +0 -142
- data/lib/hobo/lifecycles/creator.rb +0 -72
- data/lib/hobo/lifecycles/lifecycle.rb +0 -249
- data/lib/hobo/lifecycles/state.rb +0 -22
- data/lib/hobo/lifecycles/transition.rb +0 -70
- data/lib/hobo/model_router.rb +0 -290
- data/lib/hobo/permissions.rb +0 -451
- data/lib/hobo/permissions/associations.rb +0 -175
- data/lib/hobo/rapid_helper.rb +0 -157
- data/lib/hobo/scopes.rb +0 -43
- data/lib/hobo/scopes/apply_scopes.rb +0 -23
- data/lib/hobo/scopes/association_proxy_extensions.rb +0 -62
- data/lib/hobo/scopes/automatic_scopes.rb +0 -421
- data/lib/hobo/scopes/named_scope_extensions.rb +0 -39
- data/lib/hobo/tasks/rails.rb +0 -4
- data/lib/hobo/translations.rb +0 -93
- data/lib/hobo/undefined_access_error.rb +0 -5
- data/lib/hobo/user.rb +0 -182
- data/lib/hobo/view_hints.rb +0 -115
- data/rails/init.rb +0 -10
- data/rails_generators/hobo/USAGE +0 -4
- data/rails_generators/hobo/hobo_generator.rb +0 -53
- data/rails_generators/hobo/templates/application.dryml +0 -1
- data/rails_generators/hobo/templates/initializer.rb +0 -2
- data/rails_generators/hobo_admin_site/USAGE +0 -16
- data/rails_generators/hobo_admin_site/hobo_admin_site_generator.rb +0 -45
- data/rails_generators/hobo_admin_site/templates/admin.css +0 -2
- data/rails_generators/hobo_admin_site/templates/users_index.dryml +0 -5
- data/rails_generators/hobo_front_controller/hobo_front_controller_generator.rb +0 -95
- data/rails_generators/hobo_front_controller/templates/functional_test.rb +0 -8
- data/rails_generators/hobo_front_controller/templates/helper.rb +0 -2
- data/rails_generators/hobo_model/USAGE +0 -25
- data/rails_generators/hobo_model/hobo_model_generator.rb +0 -43
- data/rails_generators/hobo_model/templates/fixtures.yml +0 -6
- data/rails_generators/hobo_model/templates/hints.rb +0 -7
- data/rails_generators/hobo_model/templates/unit_test.rb +0 -8
- data/rails_generators/hobo_model_controller/USAGE +0 -30
- data/rails_generators/hobo_model_controller/hobo_model_controller_generator.rb +0 -46
- data/rails_generators/hobo_model_controller/templates/controller.rb +0 -7
- data/rails_generators/hobo_model_controller/templates/functional_test.rb +0 -8
- data/rails_generators/hobo_model_controller/templates/helper.rb +0 -2
- data/rails_generators/hobo_model_resource/USAGE +0 -38
- data/rails_generators/hobo_model_resource/hobo_model_resource_generator.rb +0 -73
- data/rails_generators/hobo_model_resource/templates/controller.rb +0 -7
- data/rails_generators/hobo_model_resource/templates/functional_test.rb +0 -8
- data/rails_generators/hobo_model_resource/templates/helper.rb +0 -2
- data/rails_generators/hobo_rapid/hobo_rapid_generator.rb +0 -94
- data/rails_generators/hobo_subsite/USAGE +0 -16
- data/rails_generators/hobo_subsite/hobo_subsite_generator.rb +0 -73
- data/rails_generators/hobo_subsite/templates/site_taglib.dryml +0 -13
- data/rails_generators/hobo_user_controller/USAGE +0 -34
- data/rails_generators/hobo_user_controller/hobo_user_controller_generator.rb +0 -65
- data/rails_generators/hobo_user_controller/templates/controller.rb +0 -29
- data/rails_generators/hobo_user_controller/templates/functional_test.rb +0 -8
- data/rails_generators/hobo_user_controller/templates/helper.rb +0 -2
- data/rails_generators/hobo_user_model/USAGE +0 -16
- data/rails_generators/hobo_user_model/hobo_user_model_generator.rb +0 -46
- data/rails_generators/hobo_user_model/templates/fixtures.yml +0 -19
- data/rails_generators/hobo_user_model/templates/mailer.rb +0 -29
- data/rails_generators/hobo_user_model/templates/unit_test.rb +0 -8
- data/script/destroy +0 -14
- data/script/generate +0 -14
- data/taglibs/rapid_translations.dryml +0 -36
- data/tasks/environments.rake +0 -19
- data/tasks/hobo_tasks.rake +0 -58
- data/test/generators/test_generator_helper.rb +0 -29
- data/test/generators/test_helper.rb +0 -1
- data/test/generators/test_hobo_model_controller_generator.rb +0 -56
@@ -0,0 +1,38 @@
|
|
1
|
+
module Hobo
|
2
|
+
module Model
|
3
|
+
module Scopes
|
4
|
+
|
5
|
+
def self.included_in_class(klass)
|
6
|
+
klass.class_eval do
|
7
|
+
extend ClassMethods
|
8
|
+
metaclass.alias_method_chain :valid_keys_for_has_many_association, :scopes
|
9
|
+
metaclass.alias_method_chain :valid_keys_for_has_one_association, :scopes
|
10
|
+
metaclass.alias_method_chain :valid_keys_for_belongs_to_association, :scopes
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
|
16
|
+
include AutomaticScopes
|
17
|
+
|
18
|
+
include ApplyScopes
|
19
|
+
|
20
|
+
# --- monkey-patches to allow :scope key on has_many, has_one and belongs_to ---
|
21
|
+
|
22
|
+
def valid_keys_for_has_many_association_with_scopes
|
23
|
+
valid_keys_for_has_many_association_without_scopes + [:scope]
|
24
|
+
end
|
25
|
+
|
26
|
+
def valid_keys_for_has_one_association_with_scopes
|
27
|
+
valid_keys_for_has_one_association_without_scopes + [:scope]
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid_keys_for_belongs_to_association_with_scopes
|
31
|
+
valid_keys_for_belongs_to_association_without_scopes + [:scope]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Hobo
|
2
|
+
module Model
|
3
|
+
module Scopes
|
4
|
+
module ApplyScopes
|
5
|
+
|
6
|
+
def apply_scopes(scopes)
|
7
|
+
result = scoped
|
8
|
+
scopes.each_pair do |scope, arg|
|
9
|
+
if arg.is_a?(Array)
|
10
|
+
result = result.send(scope, *arg) unless arg.first.blank?
|
11
|
+
else
|
12
|
+
result = result.send(scope, arg) unless arg.blank?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
result
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,428 @@
|
|
1
|
+
module Hobo
|
2
|
+
module Model
|
3
|
+
module Scopes
|
4
|
+
|
5
|
+
module AutomaticScopes
|
6
|
+
|
7
|
+
def create_automatic_scope(name, check_only=false)
|
8
|
+
ScopeBuilder.new(self, name).create_scope(check_only)
|
9
|
+
rescue ActiveRecord::StatementInvalid => e
|
10
|
+
# Problem with the database? Don't try to create automatic
|
11
|
+
# scopes
|
12
|
+
if ActiveRecord::Base.logger
|
13
|
+
ActiveRecord::Base.logger.warn "!! Database exception during hobo auto-scope creation -- continuing automatic scopes"
|
14
|
+
ActiveRecord::Base.logger.warn "!! #{e.to_s}"
|
15
|
+
end
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
# The methods on this module add scopes to the given class
|
22
|
+
class ScopeBuilder
|
23
|
+
|
24
|
+
def initialize(klass, name)
|
25
|
+
@klass = klass
|
26
|
+
@name = name.to_s
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_reader :name
|
30
|
+
|
31
|
+
def create_scope(check_only=false)
|
32
|
+
matched_scope = true
|
33
|
+
|
34
|
+
case
|
35
|
+
# --- Association Queries --- #
|
36
|
+
|
37
|
+
# with_players(player1, player2)
|
38
|
+
when name =~ /^with_(.*)/ && (refl = reflection($1))
|
39
|
+
return true if check_only
|
40
|
+
|
41
|
+
def_scope do |*records|
|
42
|
+
if records.empty?
|
43
|
+
@klass.where exists_sql_condition(refl, true)
|
44
|
+
else
|
45
|
+
records = records.flatten.compact.map {|r| find_if_named(refl, r) }
|
46
|
+
exists_sql = ([exists_sql_condition(refl)] * records.length).join(" AND ")
|
47
|
+
@klass.where *([exists_sql] + records)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# with_player(a_player)
|
52
|
+
when name =~ /^with_(.*)/ && (refl = reflection($1.pluralize))
|
53
|
+
return true if check_only
|
54
|
+
|
55
|
+
exists_sql = exists_sql_condition(refl)
|
56
|
+
def_scope do |record|
|
57
|
+
record = find_if_named(refl, record)
|
58
|
+
@klass.where exists_sql, record
|
59
|
+
end
|
60
|
+
|
61
|
+
# any_of_players(player1, player2)
|
62
|
+
when name =~ /^any_of_(.*)/ && (refl = reflection($1))
|
63
|
+
return true if check_only
|
64
|
+
|
65
|
+
def_scope do |*records|
|
66
|
+
if records.empty?
|
67
|
+
@klass.where exists_sql_condition(refl, true)
|
68
|
+
else
|
69
|
+
records = records.flatten.compact.map {|r| find_if_named(refl, r) }
|
70
|
+
exists_sql = ([exists_sql_condition(refl)] * records.length).join(" OR ")
|
71
|
+
@klass.where *([exists_sql] + records)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# without_players(player1, player2)
|
76
|
+
when name =~ /^without_(.*)/ && (refl = reflection($1))
|
77
|
+
return true if check_only
|
78
|
+
|
79
|
+
def_scope do |*records|
|
80
|
+
if records.empty?
|
81
|
+
@klass.where "NOT (#{exists_sql_condition(refl, true)})"
|
82
|
+
else
|
83
|
+
records = records.flatten.compact.map {|r| find_if_named(refl, r) }
|
84
|
+
exists_sql = ([exists_sql_condition(refl)] * records.length).join(" AND ")
|
85
|
+
@klass.where *(["NOT (#{exists_sql})"] + records)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# without_player(a_player)
|
90
|
+
when name =~ /^without_(.*)/ && (refl = reflection($1.pluralize))
|
91
|
+
return true if check_only
|
92
|
+
|
93
|
+
exists_sql = exists_sql_condition(refl)
|
94
|
+
def_scope do |record|
|
95
|
+
record = find_if_named(refl, record)
|
96
|
+
@klass.where "NOT #{exists_sql}", record
|
97
|
+
end
|
98
|
+
|
99
|
+
# team_is(a_team)
|
100
|
+
when name =~ /^(.*)_is$/ && (refl = reflection($1)) && refl.macro.in?([:has_one, :belongs_to])
|
101
|
+
return true if check_only
|
102
|
+
|
103
|
+
if refl.options[:polymorphic]
|
104
|
+
def_scope do |record|
|
105
|
+
record = find_if_named(refl, record)
|
106
|
+
@klass.where "#{foreign_key_column refl} = ? AND #{$1}_type = ?", record, record.class.name
|
107
|
+
end
|
108
|
+
else
|
109
|
+
def_scope do |record|
|
110
|
+
record = find_if_named(refl, record)
|
111
|
+
@klass.where "#{foreign_key_column refl} = ?", record
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# team_is_not(a_team)
|
116
|
+
when name =~ /^(.*)_is_not$/ && (refl = reflection($1)) && refl.macro.in?([:has_one, :belongs_to])
|
117
|
+
return true if check_only
|
118
|
+
|
119
|
+
if refl.options[:polymorphic]
|
120
|
+
def_scope do |record|
|
121
|
+
record = find_if_named(refl, record)
|
122
|
+
@klass.where "#{foreign_key_column refl} <> ? OR #{name}_type <> ?", record, record.class.name
|
123
|
+
end
|
124
|
+
else
|
125
|
+
def_scope do |record|
|
126
|
+
record = find_if_named(refl, record)
|
127
|
+
@klass.where "#{foreign_key_column refl} <> ?", record
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
# --- Column Queries --- #
|
133
|
+
|
134
|
+
# name_is(str)
|
135
|
+
when name =~ /^(.*)_is$/ && (col = column($1))
|
136
|
+
return true if check_only
|
137
|
+
|
138
|
+
def_scope do |str|
|
139
|
+
@klass.where "#{column_sql(col)} = ?", str
|
140
|
+
end
|
141
|
+
|
142
|
+
# name_is_not(str)
|
143
|
+
when name =~ /^(.*)_is_not$/ && (col = column($1))
|
144
|
+
return true if check_only
|
145
|
+
|
146
|
+
def_scope do |str|
|
147
|
+
@klass.where "#{column_sql(col)} <> ?", str
|
148
|
+
end
|
149
|
+
|
150
|
+
# name_contains(str)
|
151
|
+
when name =~ /^(.*)_contains$/ && (col = column($1))
|
152
|
+
return true if check_only
|
153
|
+
|
154
|
+
def_scope do |str|
|
155
|
+
@klass.where "#{column_sql(col)} LIKE ?", "%#{str}%"
|
156
|
+
end
|
157
|
+
|
158
|
+
# name_does_not_contain
|
159
|
+
when name =~ /^(.*)_does_not_contain$/ && (col = column($1))
|
160
|
+
return true if check_only
|
161
|
+
|
162
|
+
def_scope do |str|
|
163
|
+
@klass.where "#{column_sql(col)} NOT LIKE ?", "%#{str}%"
|
164
|
+
end
|
165
|
+
|
166
|
+
# name_starts(str)
|
167
|
+
when name =~ /^(.*)_starts$/ && (col = column($1))
|
168
|
+
return true if check_only
|
169
|
+
|
170
|
+
def_scope do |str|
|
171
|
+
@klass.where "#{column_sql(col)} LIKE ?", "#{str}%"
|
172
|
+
end
|
173
|
+
|
174
|
+
# name_does_not_start
|
175
|
+
when name =~ /^(.*)_does_not_start$/ && (col = column($1))
|
176
|
+
return true if check_only
|
177
|
+
|
178
|
+
def_scope do |str|
|
179
|
+
@klass.where "#{column_sql(col)} NOT LIKE ?", "#{str}%"
|
180
|
+
end
|
181
|
+
|
182
|
+
# name_ends(str)
|
183
|
+
when name =~ /^(.*)_ends$/ && (col = column($1))
|
184
|
+
return true if check_only
|
185
|
+
|
186
|
+
def_scope do |str|
|
187
|
+
@klass.where "#{column_sql(col)} LIKE ?", "%#{str}"
|
188
|
+
end
|
189
|
+
|
190
|
+
# name_does_not_end(str)
|
191
|
+
when name =~ /^(.*)_does_not_end$/ && (col = column($1))
|
192
|
+
return true if check_only
|
193
|
+
|
194
|
+
def_scope do |str|
|
195
|
+
@klass.where "#{column_sql(col)} NOT LIKE ?", "%#{str}"
|
196
|
+
end
|
197
|
+
|
198
|
+
# published (a boolean column)
|
199
|
+
when (col = column(name)) && (col.type == :boolean)
|
200
|
+
return true if check_only
|
201
|
+
|
202
|
+
def_scope do
|
203
|
+
@klass.where "#{column_sql(col)} = ?", true
|
204
|
+
end
|
205
|
+
|
206
|
+
# not_published
|
207
|
+
when name =~ /^not_(.*)$/ && (col = column($1)) && (col.type == :boolean)
|
208
|
+
return true if check_only
|
209
|
+
|
210
|
+
def_scope do
|
211
|
+
@klass.where "#{column_sql(col)} <> ?", true
|
212
|
+
end
|
213
|
+
|
214
|
+
# published_before(time)
|
215
|
+
when name =~ /^(.*)_before$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
|
216
|
+
return true if check_only
|
217
|
+
|
218
|
+
def_scope do |time|
|
219
|
+
@klass.where "#{column_sql(col)} < ?", time
|
220
|
+
end
|
221
|
+
|
222
|
+
# published_after(time)
|
223
|
+
when name =~ /^(.*)_after$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
|
224
|
+
return true if check_only
|
225
|
+
|
226
|
+
def_scope do |time|
|
227
|
+
@klass.where "#{column_sql(col)} > ?", time
|
228
|
+
end
|
229
|
+
|
230
|
+
# published_between(time1, time2)
|
231
|
+
when name =~ /^(.*)_between$/ && (col = column("#{$1}_at") || column("#{$1}_date") || column("#{$1}_on")) && col.type.in?([:date, :datetime, :time, :timestamp])
|
232
|
+
return true if check_only
|
233
|
+
|
234
|
+
def_scope do |time1, time2|
|
235
|
+
@klass.where "#{column_sql(col)} >= ? AND #{column_sql(col)} <= ?", time1, time2
|
236
|
+
end
|
237
|
+
|
238
|
+
# active (a lifecycle state)
|
239
|
+
when @klass.has_lifecycle? && name.to_sym.in?(@klass::Lifecycle.state_names)
|
240
|
+
return true if check_only
|
241
|
+
|
242
|
+
if @klass::Lifecycle.state_names.length == 1
|
243
|
+
# nothing to check for - create a dummy scope
|
244
|
+
def_scope { @klass.scoped }
|
245
|
+
true
|
246
|
+
else
|
247
|
+
def_scope do
|
248
|
+
@klass.where "#{@klass.table_name}.#{@klass::Lifecycle.state_field} = ?", name
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
# self is / is not
|
253
|
+
when name == "is"
|
254
|
+
return true if check_only
|
255
|
+
|
256
|
+
def_scope do |record|
|
257
|
+
@klass.where "#{@klass.table_name}.#{@klass.primary_key} = ?", record
|
258
|
+
end
|
259
|
+
|
260
|
+
when name == "is_not"
|
261
|
+
return true if check_only
|
262
|
+
|
263
|
+
def_scope do |record|
|
264
|
+
@klass.where "#{@klass.table_name}.#{@klass.primary_key} <> ?", record
|
265
|
+
end
|
266
|
+
|
267
|
+
|
268
|
+
when name == "by_most_recent"
|
269
|
+
return true if check_only
|
270
|
+
|
271
|
+
def_scope do
|
272
|
+
@klass.order "#{@klass.table_name}.created_at DESC"
|
273
|
+
end
|
274
|
+
|
275
|
+
when name == "recent"
|
276
|
+
return true if check_only
|
277
|
+
|
278
|
+
if "created_at".in?(@klass.columns.*.name)
|
279
|
+
def_scope do |*args|
|
280
|
+
count = args.first || 6
|
281
|
+
@klass.order("#{@klass.table_name}.created_at DESC").limit(count)
|
282
|
+
end
|
283
|
+
else
|
284
|
+
def_scope do |*args|
|
285
|
+
count = args.first || 6
|
286
|
+
limit(count)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
when name == "order_by"
|
291
|
+
# DEPRECATED: use :order instead.
|
292
|
+
Rails.logger.warn "Automatic scope :order_by has been deprecated: use :order instead."
|
293
|
+
return true if check_only
|
294
|
+
|
295
|
+
klass = @klass
|
296
|
+
def_scope do |*args|
|
297
|
+
field, asc = args
|
298
|
+
type = klass.attr_type(field)
|
299
|
+
if type.nil? #a virtual attribute from an SQL alias, e.g., 'total' from 'COUNT(*) AS total'
|
300
|
+
colspec = "#{field}" # don't prepend the table name
|
301
|
+
elsif type.respond_to?(:name_attribute) && (name = type.name_attribute)
|
302
|
+
include = field
|
303
|
+
colspec = "#{type.table_name}.#{name}"
|
304
|
+
else
|
305
|
+
colspec = "#{klass.table_name}.#{field}"
|
306
|
+
end
|
307
|
+
@klass.includes(include).order("#{colspec} #{asc._?.upcase}")
|
308
|
+
end
|
309
|
+
|
310
|
+
when name == "include"
|
311
|
+
# DEPRECATED: it clashes with Module.include when called on an ActiveRecord::Relation
|
312
|
+
# after a scope chain, if you didn't call it on the class itself first
|
313
|
+
Rails.logger.warn "Automatic scope :include has been deprecated: use :includes instead."
|
314
|
+
return true if check_only
|
315
|
+
|
316
|
+
def_scope do |inclusions|
|
317
|
+
@klass.includes(inclusions)
|
318
|
+
end
|
319
|
+
|
320
|
+
when name == "search"
|
321
|
+
return true if check_only
|
322
|
+
|
323
|
+
def_scope do |query, *fields|
|
324
|
+
match_keyword = ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" ? "ILIKE" : "LIKE"
|
325
|
+
|
326
|
+
words = query.split
|
327
|
+
args = []
|
328
|
+
word_queries = words.map do |word|
|
329
|
+
field_query = '(' + fields.map { |field| "(#{@klass.table_name}.#{field} #{match_keyword} ?)" }.join(" OR ") + ')'
|
330
|
+
args += ["%#{word}%"] * fields.length
|
331
|
+
field_query
|
332
|
+
end
|
333
|
+
|
334
|
+
@klass.where *([word_queries.join(" AND ")] + args)
|
335
|
+
end
|
336
|
+
|
337
|
+
else
|
338
|
+
matched_scope = false
|
339
|
+
end
|
340
|
+
|
341
|
+
matched_scope
|
342
|
+
end
|
343
|
+
|
344
|
+
|
345
|
+
def column_sql(column)
|
346
|
+
"#{@klass.table_name}.#{column.name}"
|
347
|
+
end
|
348
|
+
|
349
|
+
|
350
|
+
def exists_sql_condition(reflection, any=false)
|
351
|
+
owner = @klass
|
352
|
+
owner_primary_key = "#{owner.table_name}.#{owner.primary_key}"
|
353
|
+
|
354
|
+
if reflection.options[:through]
|
355
|
+
join_table = reflection.through_reflection.klass.table_name
|
356
|
+
owner_fkey = reflection.through_reflection.primary_key_name
|
357
|
+
conditions = reflection.options[:conditions].blank? ? '' : " AND #{reflection.through_reflection.klass.send(:sanitize_sql_for_conditions, reflection.options[:conditions])}"
|
358
|
+
|
359
|
+
if any
|
360
|
+
"EXISTS (SELECT * FROM #{join_table} WHERE #{join_table}.#{owner_fkey} = #{owner_primary_key}#{conditions})"
|
361
|
+
else
|
362
|
+
source_fkey = reflection.source_reflection.primary_key_name
|
363
|
+
"EXISTS (SELECT * FROM #{join_table} " +
|
364
|
+
"WHERE #{join_table}.#{source_fkey} = ? AND #{join_table}.#{owner_fkey} = #{owner_primary_key}#{conditions})"
|
365
|
+
end
|
366
|
+
else
|
367
|
+
foreign_key = reflection.primary_key_name
|
368
|
+
related = reflection.klass
|
369
|
+
conditions = reflection.options[:conditions].blank? ? '' : " AND #{reflection.klass.send(:sanitize_sql_for_conditions, reflection.options[:conditions])}"
|
370
|
+
|
371
|
+
if any
|
372
|
+
"EXISTS (SELECT * FROM #{related.table_name} " +
|
373
|
+
"WHERE #{related.table_name}.#{foreign_key} = #{owner_primary_key}#{conditions})"
|
374
|
+
else
|
375
|
+
"EXISTS (SELECT * FROM #{related.table_name} " +
|
376
|
+
"WHERE #{related.table_name}.#{foreign_key} = #{owner_primary_key} AND " +
|
377
|
+
"#{related.table_name}.#{related.primary_key} = ?#{conditions})"
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
def find_if_named(reflection, string_or_record)
|
383
|
+
if string_or_record.is_a?(String)
|
384
|
+
name = string_or_record
|
385
|
+
reflection.klass.named(name)
|
386
|
+
else
|
387
|
+
string_or_record # a record
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
|
392
|
+
def column(name)
|
393
|
+
@klass.column(name)
|
394
|
+
end
|
395
|
+
|
396
|
+
|
397
|
+
def reflection(name)
|
398
|
+
@klass.reflections[name.to_sym]
|
399
|
+
end
|
400
|
+
|
401
|
+
|
402
|
+
def def_scope(&block)
|
403
|
+
_name = name.to_sym
|
404
|
+
@klass.scope _name, (lambda &block)
|
405
|
+
# this is tricky; ordinarily, we'd worry about subclasses that haven't yet been loaded.
|
406
|
+
# HOWEVER, they will pick up the scope setting via read_inheritable_attribute when they do
|
407
|
+
# load, so only the currently existing subclasses need to be fixed up.
|
408
|
+
_scope = @klass.scopes[_name]
|
409
|
+
@klass.send(:descendants).each do |k|
|
410
|
+
k.scopes[_name] = _scope
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
414
|
+
|
415
|
+
def primary_key_column(refl)
|
416
|
+
"#{refl.klass.table_name}.#{refl.klass.primary_key}"
|
417
|
+
end
|
418
|
+
|
419
|
+
|
420
|
+
def foreign_key_column(refl)
|
421
|
+
"#{@klass.table_name}.#{refl.primary_key_name}"
|
422
|
+
end
|
423
|
+
|
424
|
+
end
|
425
|
+
|
426
|
+
end
|
427
|
+
end
|
428
|
+
end
|