active_cached_resource 0.0.1.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +22 -0
- data/.standard.yml +2 -0
- data/CHANGELOG.md +5 -0
- data/README.md +45 -0
- data/Rakefile +21 -0
- data/example/consumer/.dockerignore +41 -0
- data/example/consumer/.gitattributes +9 -0
- data/example/consumer/.gitignore +36 -0
- data/example/consumer/.kamal/hooks/docker-setup.sample +3 -0
- data/example/consumer/.kamal/hooks/post-deploy.sample +14 -0
- data/example/consumer/.kamal/hooks/post-proxy-reboot.sample +3 -0
- data/example/consumer/.kamal/hooks/pre-build.sample +51 -0
- data/example/consumer/.kamal/hooks/pre-connect.sample +47 -0
- data/example/consumer/.kamal/hooks/pre-deploy.sample +109 -0
- data/example/consumer/.kamal/hooks/pre-proxy-reboot.sample +3 -0
- data/example/consumer/.kamal/secrets +17 -0
- data/example/consumer/Dockerfile +65 -0
- data/example/consumer/Gemfile +17 -0
- data/example/consumer/Rakefile +6 -0
- data/example/consumer/app/controllers/application_controller.rb +2 -0
- data/example/consumer/app/controllers/concerns/.keep +0 -0
- data/example/consumer/app/jobs/application_job.rb +7 -0
- data/example/consumer/app/mailers/application_mailer.rb +4 -0
- data/example/consumer/app/models/application_record.rb +3 -0
- data/example/consumer/app/models/concerns/.keep +0 -0
- data/example/consumer/app/models/person.rb +9 -0
- data/example/consumer/app/views/layouts/mailer.html.erb +13 -0
- data/example/consumer/app/views/layouts/mailer.text.erb +1 -0
- data/example/consumer/bin/brakeman +7 -0
- data/example/consumer/bin/bundle +109 -0
- data/example/consumer/bin/dev +2 -0
- data/example/consumer/bin/docker-entrypoint +14 -0
- data/example/consumer/bin/jobs +6 -0
- data/example/consumer/bin/kamal +27 -0
- data/example/consumer/bin/rails +4 -0
- data/example/consumer/bin/rake +4 -0
- data/example/consumer/bin/rubocop +8 -0
- data/example/consumer/bin/setup +34 -0
- data/example/consumer/bin/thrust +5 -0
- data/example/consumer/config/application.rb +20 -0
- data/example/consumer/config/boot.rb +3 -0
- data/example/consumer/config/cache.yml +16 -0
- data/example/consumer/config/credentials.yml.enc +1 -0
- data/example/consumer/config/database.yml +14 -0
- data/example/consumer/config/deploy.yml +116 -0
- data/example/consumer/config/environment.rb +5 -0
- data/example/consumer/config/environments/development.rb +64 -0
- data/example/consumer/config/environments/production.rb +85 -0
- data/example/consumer/config/environments/test.rb +50 -0
- data/example/consumer/config/initializers/cors.rb +16 -0
- data/example/consumer/config/initializers/filter_parameter_logging.rb +8 -0
- data/example/consumer/config/initializers/inflections.rb +16 -0
- data/example/consumer/config/locales/en.yml +31 -0
- data/example/consumer/config/puma.rb +41 -0
- data/example/consumer/config/queue.yml +18 -0
- data/example/consumer/config/recurring.yml +10 -0
- data/example/consumer/config/routes.rb +10 -0
- data/example/consumer/config.ru +6 -0
- data/example/consumer/db/cache_schema.rb +14 -0
- data/example/consumer/db/queue_schema.rb +129 -0
- data/example/consumer/db/seeds.rb +0 -0
- data/example/consumer/lib/tasks/.keep +0 -0
- data/example/consumer/log/.keep +0 -0
- data/example/consumer/public/robots.txt +1 -0
- data/example/consumer/script/.keep +0 -0
- data/example/consumer/storage/.keep +0 -0
- data/example/consumer/tmp/.keep +0 -0
- data/example/consumer/tmp/cache/.keep +0 -0
- data/example/consumer/tmp/pids/.keep +0 -0
- data/example/consumer/tmp/storage/.keep +0 -0
- data/example/consumer/vendor/.keep +0 -0
- data/example/provider/.dockerignore +41 -0
- data/example/provider/.gitattributes +9 -0
- data/example/provider/.gitignore +32 -0
- data/example/provider/.kamal/hooks/docker-setup.sample +3 -0
- data/example/provider/.kamal/hooks/post-deploy.sample +14 -0
- data/example/provider/.kamal/hooks/post-proxy-reboot.sample +3 -0
- data/example/provider/.kamal/hooks/pre-build.sample +51 -0
- data/example/provider/.kamal/hooks/pre-connect.sample +47 -0
- data/example/provider/.kamal/hooks/pre-deploy.sample +109 -0
- data/example/provider/.kamal/hooks/pre-proxy-reboot.sample +3 -0
- data/example/provider/.kamal/secrets +17 -0
- data/example/provider/Dockerfile +65 -0
- data/example/provider/Gemfile +14 -0
- data/example/provider/Rakefile +6 -0
- data/example/provider/app/controllers/application_controller.rb +2 -0
- data/example/provider/app/controllers/concerns/.keep +0 -0
- data/example/provider/app/controllers/people_controller.rb +68 -0
- data/example/provider/app/jobs/application_job.rb +7 -0
- data/example/provider/app/mailers/application_mailer.rb +4 -0
- data/example/provider/app/models/address.rb +3 -0
- data/example/provider/app/models/application_record.rb +3 -0
- data/example/provider/app/models/company.rb +3 -0
- data/example/provider/app/models/concerns/.keep +0 -0
- data/example/provider/app/models/person.rb +6 -0
- data/example/provider/app/views/layouts/mailer.html.erb +13 -0
- data/example/provider/app/views/layouts/mailer.text.erb +1 -0
- data/example/provider/bin/brakeman +7 -0
- data/example/provider/bin/bundle +109 -0
- data/example/provider/bin/dev +2 -0
- data/example/provider/bin/docker-entrypoint +14 -0
- data/example/provider/bin/jobs +6 -0
- data/example/provider/bin/kamal +27 -0
- data/example/provider/bin/rails +4 -0
- data/example/provider/bin/rake +4 -0
- data/example/provider/bin/rubocop +8 -0
- data/example/provider/bin/setup +34 -0
- data/example/provider/bin/thrust +5 -0
- data/example/provider/config/application.rb +44 -0
- data/example/provider/config/boot.rb +3 -0
- data/example/provider/config/cache.yml +16 -0
- data/example/provider/config/credentials.yml.enc +1 -0
- data/example/provider/config/database.yml +20 -0
- data/example/provider/config/deploy.yml +116 -0
- data/example/provider/config/environment.rb +5 -0
- data/example/provider/config/environments/development.rb +64 -0
- data/example/provider/config/environments/production.rb +85 -0
- data/example/provider/config/environments/test.rb +50 -0
- data/example/provider/config/initializers/cors.rb +16 -0
- data/example/provider/config/initializers/filter_parameter_logging.rb +8 -0
- data/example/provider/config/initializers/inflections.rb +16 -0
- data/example/provider/config/locales/en.yml +31 -0
- data/example/provider/config/puma.rb +41 -0
- data/example/provider/config/queue.yml +18 -0
- data/example/provider/config/recurring.yml +10 -0
- data/example/provider/config/routes.rb +4 -0
- data/example/provider/config.ru +6 -0
- data/example/provider/db/cache_schema.rb +14 -0
- data/example/provider/db/migrate/20241202183937_create_people.rb +11 -0
- data/example/provider/db/migrate/20241202183955_create_addresses.rb +13 -0
- data/example/provider/db/migrate/20241202184017_create_companies.rb +14 -0
- data/example/provider/db/queue_schema.rb +129 -0
- data/example/provider/db/schema.rb +47 -0
- data/example/provider/db/seeds.rb +18 -0
- data/example/provider/lib/tasks/.keep +0 -0
- data/example/provider/log/.keep +0 -0
- data/example/provider/public/robots.txt +1 -0
- data/example/provider/script/.keep +0 -0
- data/example/provider/storage/.keep +0 -0
- data/example/provider/tmp/.keep +0 -0
- data/example/provider/tmp/pids/.keep +0 -0
- data/example/provider/tmp/storage/.keep +0 -0
- data/example/provider/vendor/.keep +0 -0
- data/lib/active_cached_resource/caching.rb +176 -0
- data/lib/active_cached_resource/caching_strategies/active_support_cache.rb +31 -0
- data/lib/active_cached_resource/caching_strategies/base.rb +114 -0
- data/lib/active_cached_resource/caching_strategies/sql_cache.rb +32 -0
- data/lib/active_cached_resource/configuration.rb +50 -0
- data/lib/active_cached_resource/logger.rb +22 -0
- data/lib/active_cached_resource/model.rb +33 -0
- data/lib/active_cached_resource/version.rb +12 -0
- data/lib/active_cached_resource.rb +64 -0
- data/lib/activeresource/.gitignore +15 -0
- data/lib/activeresource/README.md +283 -0
- data/lib/activeresource/examples/performance.rb +72 -0
- data/lib/activeresource/lib/active_resource/active_job_serializer.rb +26 -0
- data/lib/activeresource/lib/active_resource/associations/builder/association.rb +32 -0
- data/lib/activeresource/lib/active_resource/associations/builder/belongs_to.rb +16 -0
- data/lib/activeresource/lib/active_resource/associations/builder/has_many.rb +14 -0
- data/lib/activeresource/lib/active_resource/associations/builder/has_one.rb +14 -0
- data/lib/activeresource/lib/active_resource/associations.rb +175 -0
- data/lib/activeresource/lib/active_resource/base.rb +1741 -0
- data/lib/activeresource/lib/active_resource/callbacks.rb +22 -0
- data/lib/activeresource/lib/active_resource/collection.rb +214 -0
- data/lib/activeresource/lib/active_resource/connection.rb +298 -0
- data/lib/activeresource/lib/active_resource/custom_methods.rb +129 -0
- data/lib/activeresource/lib/active_resource/exceptions.rb +98 -0
- data/lib/activeresource/lib/active_resource/formats/json_format.rb +28 -0
- data/lib/activeresource/lib/active_resource/formats/xml_format.rb +27 -0
- data/lib/activeresource/lib/active_resource/formats.rb +24 -0
- data/lib/activeresource/lib/active_resource/http_mock.rb +386 -0
- data/lib/activeresource/lib/active_resource/inheriting_hash.rb +34 -0
- data/lib/activeresource/lib/active_resource/log_subscriber.rb +26 -0
- data/lib/activeresource/lib/active_resource/railtie.rb +31 -0
- data/lib/activeresource/lib/active_resource/reflection.rb +78 -0
- data/lib/activeresource/lib/active_resource/schema.rb +60 -0
- data/lib/activeresource/lib/active_resource/singleton.rb +111 -0
- data/lib/activeresource/lib/active_resource/threadsafe_attributes.rb +65 -0
- data/lib/activeresource/lib/active_resource/validations.rb +176 -0
- data/lib/activeresource/lib/active_resource.rb +49 -0
- data/lib/activeresource/lib/activeresource.rb +3 -0
- data/lib/activeresource/test/abstract_unit.rb +153 -0
- data/lib/activeresource/test/cases/active_job_serializer_test.rb +53 -0
- data/lib/activeresource/test/cases/association_test.rb +104 -0
- data/lib/activeresource/test/cases/associations/builder/belongs_to_test.rb +42 -0
- data/lib/activeresource/test/cases/associations/builder/has_many_test.rb +28 -0
- data/lib/activeresource/test/cases/associations/builder/has_one_test.rb +28 -0
- data/lib/activeresource/test/cases/authorization_test.rb +276 -0
- data/lib/activeresource/test/cases/base/custom_methods_test.rb +155 -0
- data/lib/activeresource/test/cases/base/equality_test.rb +53 -0
- data/lib/activeresource/test/cases/base/load_test.rb +249 -0
- data/lib/activeresource/test/cases/base/schema_test.rb +428 -0
- data/lib/activeresource/test/cases/base_errors_test.rb +129 -0
- data/lib/activeresource/test/cases/base_test.rb +1622 -0
- data/lib/activeresource/test/cases/callbacks_test.rb +155 -0
- data/lib/activeresource/test/cases/collection_test.rb +172 -0
- data/lib/activeresource/test/cases/connection_test.rb +357 -0
- data/lib/activeresource/test/cases/finder_test.rb +217 -0
- data/lib/activeresource/test/cases/format_test.rb +137 -0
- data/lib/activeresource/test/cases/http_mock_test.rb +213 -0
- data/lib/activeresource/test/cases/inheritence_test.rb +19 -0
- data/lib/activeresource/test/cases/inheriting_hash_test.rb +25 -0
- data/lib/activeresource/test/cases/log_subscriber_test.rb +63 -0
- data/lib/activeresource/test/cases/reflection_test.rb +65 -0
- data/lib/activeresource/test/cases/validations_test.rb +78 -0
- data/lib/activeresource/test/fixtures/address.rb +20 -0
- data/lib/activeresource/test/fixtures/beast.rb +16 -0
- data/lib/activeresource/test/fixtures/comment.rb +5 -0
- data/lib/activeresource/test/fixtures/customer.rb +5 -0
- data/lib/activeresource/test/fixtures/inventory.rb +14 -0
- data/lib/activeresource/test/fixtures/person.rb +15 -0
- data/lib/activeresource/test/fixtures/pet.rb +6 -0
- data/lib/activeresource/test/fixtures/post.rb +5 -0
- data/lib/activeresource/test/fixtures/product.rb +11 -0
- data/lib/activeresource/test/fixtures/project.rb +19 -0
- data/lib/activeresource/test/fixtures/proxy.rb +6 -0
- data/lib/activeresource/test/fixtures/sound.rb +11 -0
- data/lib/activeresource/test/fixtures/street_address.rb +6 -0
- data/lib/activeresource/test/fixtures/subscription_plan.rb +7 -0
- data/lib/activeresource/test/fixtures/weather.rb +21 -0
- data/lib/activeresource/test/setter_trap.rb +28 -0
- data/lib/activeresource/test/singleton_test.rb +138 -0
- data/lib/activeresource/test/threadsafe_attributes_test.rb +91 -0
- data/lib/generators/active_cached_resource/install_generator.rb +31 -0
- data/lib/generators/active_cached_resource/templates/migration.erb +16 -0
- data/sorbet/config +4 -0
- data/sorbet/rbi/annotations/.gitattributes +1 -0
- data/sorbet/rbi/annotations/activemodel.rbi +89 -0
- data/sorbet/rbi/annotations/activesupport.rbi +457 -0
- data/sorbet/rbi/annotations/minitest.rbi +119 -0
- data/sorbet/rbi/annotations/rainbow.rbi +269 -0
- data/sorbet/rbi/dsl/.gitattributes +1 -0
- data/sorbet/rbi/dsl/active_support/callbacks.rbi +21 -0
- data/sorbet/rbi/gems/.gitattributes +1 -0
- data/sorbet/rbi/gems/actioncable@8.0.0.rbi +252 -0
- data/sorbet/rbi/gems/actionmailbox@8.0.0.rbi +9 -0
- data/sorbet/rbi/gems/actionmailer@8.0.0.rbi +9 -0
- data/sorbet/rbi/gems/actionpack@8.0.0.rbi +20909 -0
- data/sorbet/rbi/gems/actiontext@8.0.0.rbi +9 -0
- data/sorbet/rbi/gems/actionview@8.0.0.rbi +16207 -0
- data/sorbet/rbi/gems/activejob@8.0.0.rbi +9 -0
- data/sorbet/rbi/gems/activemodel-serializers-xml@1.0.3.rbi +166 -0
- data/sorbet/rbi/gems/activemodel@8.0.0.rbi +6857 -0
- data/sorbet/rbi/gems/activerecord@8.0.0.rbi +42896 -0
- data/sorbet/rbi/gems/activeresource@6.1.4.rbi +3944 -0
- data/sorbet/rbi/gems/activestorage@8.0.0.rbi +9 -0
- data/sorbet/rbi/gems/activesupport@8.0.0.rbi +21251 -0
- data/sorbet/rbi/gems/ast@2.4.2.rbi +585 -0
- data/sorbet/rbi/gems/base64@0.2.0.rbi +509 -0
- data/sorbet/rbi/gems/benchmark@0.4.0.rbi +618 -0
- data/sorbet/rbi/gems/bigdecimal@3.1.8.rbi +78 -0
- data/sorbet/rbi/gems/builder@3.3.0.rbi +9 -0
- data/sorbet/rbi/gems/bump@0.10.0.rbi +169 -0
- data/sorbet/rbi/gems/byebug@11.1.3.rbi +3607 -0
- data/sorbet/rbi/gems/coderay@1.1.3.rbi +3427 -0
- data/sorbet/rbi/gems/concurrent-ruby@1.3.4.rbi +11645 -0
- data/sorbet/rbi/gems/connection_pool@2.4.1.rbi +9 -0
- data/sorbet/rbi/gems/crass@1.0.6.rbi +623 -0
- data/sorbet/rbi/gems/date@3.4.0.rbi +75 -0
- data/sorbet/rbi/gems/diff-lcs@1.5.1.rbi +1131 -0
- data/sorbet/rbi/gems/docile@1.4.1.rbi +377 -0
- data/sorbet/rbi/gems/drb@2.2.1.rbi +1347 -0
- data/sorbet/rbi/gems/erubi@1.13.0.rbi +150 -0
- data/sorbet/rbi/gems/globalid@1.2.1.rbi +9 -0
- data/sorbet/rbi/gems/i18n@1.14.6.rbi +2359 -0
- data/sorbet/rbi/gems/io-console@0.7.2.rbi +9 -0
- data/sorbet/rbi/gems/json@2.8.2.rbi +1901 -0
- data/sorbet/rbi/gems/language_server-protocol@3.17.0.3.rbi +14238 -0
- data/sorbet/rbi/gems/lint_roller@1.1.0.rbi +240 -0
- data/sorbet/rbi/gems/logger@1.6.1.rbi +920 -0
- data/sorbet/rbi/gems/loofah@2.23.1.rbi +1081 -0
- data/sorbet/rbi/gems/mail@2.8.1.rbi +9 -0
- data/sorbet/rbi/gems/marcel@1.0.4.rbi +9 -0
- data/sorbet/rbi/gems/method_source@1.1.0.rbi +304 -0
- data/sorbet/rbi/gems/mini_mime@1.1.5.rbi +9 -0
- data/sorbet/rbi/gems/minitest@5.25.2.rbi +1547 -0
- data/sorbet/rbi/gems/net-imap@0.5.1.rbi +9 -0
- data/sorbet/rbi/gems/net-pop@0.1.2.rbi +9 -0
- data/sorbet/rbi/gems/net-protocol@0.2.2.rbi +292 -0
- data/sorbet/rbi/gems/net-smtp@0.5.0.rbi +9 -0
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +159 -0
- data/sorbet/rbi/gems/nio4r@2.7.4.rbi +9 -0
- data/sorbet/rbi/gems/nokogiri@1.16.7.rbi +7311 -0
- data/sorbet/rbi/gems/parallel@1.26.3.rbi +291 -0
- data/sorbet/rbi/gems/parser@3.3.6.0.rbi +5519 -0
- data/sorbet/rbi/gems/prism@1.2.0.rbi +39085 -0
- data/sorbet/rbi/gems/pry-byebug@3.10.1.rbi +1151 -0
- data/sorbet/rbi/gems/pry@0.14.2.rbi +10076 -0
- data/sorbet/rbi/gems/psych@5.2.0.rbi +1785 -0
- data/sorbet/rbi/gems/racc@1.8.1.rbi +162 -0
- data/sorbet/rbi/gems/rack-session@2.0.0.rbi +727 -0
- data/sorbet/rbi/gems/rack-test@2.1.0.rbi +747 -0
- data/sorbet/rbi/gems/rack@3.1.8.rbi +4905 -0
- data/sorbet/rbi/gems/rackup@2.2.1.rbi +230 -0
- data/sorbet/rbi/gems/rails-dom-testing@2.2.0.rbi +758 -0
- data/sorbet/rbi/gems/rails-html-sanitizer@1.6.0.rbi +785 -0
- data/sorbet/rbi/gems/rails@8.0.0.rbi +9 -0
- data/sorbet/rbi/gems/railties@8.0.0.rbi +6287 -0
- data/sorbet/rbi/gems/rainbow@3.1.1.rbi +403 -0
- data/sorbet/rbi/gems/rake@13.2.1.rbi +3091 -0
- data/sorbet/rbi/gems/rbi@0.2.1.rbi +4535 -0
- data/sorbet/rbi/gems/rdoc@6.8.1.rbi +12572 -0
- data/sorbet/rbi/gems/regexp_parser@2.9.2.rbi +3772 -0
- data/sorbet/rbi/gems/reline@0.5.12.rbi +2416 -0
- data/sorbet/rbi/gems/rexml@3.3.9.rbi +4858 -0
- data/sorbet/rbi/gems/rspec-core@3.13.2.rbi +11287 -0
- data/sorbet/rbi/gems/rspec-expectations@3.13.3.rbi +8183 -0
- data/sorbet/rbi/gems/rspec-mocks@3.13.2.rbi +5341 -0
- data/sorbet/rbi/gems/rspec-support@3.13.1.rbi +1630 -0
- data/sorbet/rbi/gems/rspec@3.13.0.rbi +83 -0
- data/sorbet/rbi/gems/rubocop-ast@1.36.1.rbi +7303 -0
- data/sorbet/rbi/gems/rubocop-performance@1.21.1.rbi +9 -0
- data/sorbet/rbi/gems/rubocop@1.65.1.rbi +58170 -0
- data/sorbet/rbi/gems/ruby-progressbar@1.13.0.rbi +1318 -0
- data/sorbet/rbi/gems/securerandom@0.3.2.rbi +395 -0
- data/sorbet/rbi/gems/simplecov-html@0.13.1.rbi +225 -0
- data/sorbet/rbi/gems/simplecov@0.22.0.rbi +2149 -0
- data/sorbet/rbi/gems/simplecov_json_formatter@0.1.4.rbi +9 -0
- data/sorbet/rbi/gems/spoom@1.5.0.rbi +4932 -0
- data/sorbet/rbi/gems/standard-custom@1.0.2.rbi +9 -0
- data/sorbet/rbi/gems/standard-performance@1.4.0.rbi +9 -0
- data/sorbet/rbi/gems/standard@1.40.0.rbi +929 -0
- data/sorbet/rbi/gems/stringio@3.1.2.rbi +9 -0
- data/sorbet/rbi/gems/tapioca@0.16.4.rbi +3597 -0
- data/sorbet/rbi/gems/thor@1.3.2.rbi +4378 -0
- data/sorbet/rbi/gems/timeout@0.4.2.rbi +151 -0
- data/sorbet/rbi/gems/tzinfo@2.0.6.rbi +5918 -0
- data/sorbet/rbi/gems/unicode-display_width@2.6.0.rbi +66 -0
- data/sorbet/rbi/gems/uri@1.0.2.rbi +2377 -0
- data/sorbet/rbi/gems/useragent@0.16.10.rbi +9 -0
- data/sorbet/rbi/gems/websocket-driver@0.7.6.rbi +9 -0
- data/sorbet/rbi/gems/websocket-extensions@0.1.5.rbi +9 -0
- data/sorbet/rbi/gems/yard-sorbet@0.9.0.rbi +435 -0
- data/sorbet/rbi/gems/yard@0.9.37.rbi +18504 -0
- data/sorbet/rbi/gems/zeitwerk@2.7.1.rbi +9 -0
- data/sorbet/tapioca/config.yml +13 -0
- data/sorbet/tapioca/require.rb +12 -0
- metadata +543 -0
@@ -0,0 +1,283 @@
|
|
1
|
+
# Active Resource
|
2
|
+
|
3
|
+
**A vendored fork of [rails/activersource](https://github.com/rails/activeresource) packaged with `ActiveCachedResource`**
|
4
|
+
|
5
|
+
Active Resource (ARes) connects business objects and Representational State Transfer (REST)
|
6
|
+
web services. It implements object-relational mapping for REST web services to provide transparent
|
7
|
+
proxying capabilities between a client (Active Resource) and a RESTful service (which is provided by
|
8
|
+
Simply RESTful routing in `ActionController::Resources`).
|
9
|
+
|
10
|
+
## Philosophy
|
11
|
+
|
12
|
+
Active Resource attempts to provide a coherent wrapper object-relational mapping for REST
|
13
|
+
web services. It follows the same philosophy as Active Record, in that one of its prime aims
|
14
|
+
is to reduce the amount of code needed to map to these resources. This is made possible
|
15
|
+
by relying on a number of code- and protocol-based conventions that make it easy for Active Resource
|
16
|
+
to infer complex relations and structures. These conventions are outlined in detail in the documentation
|
17
|
+
for `ActiveResource::Base`.
|
18
|
+
|
19
|
+
## Overview
|
20
|
+
|
21
|
+
Model classes are mapped to remote REST resources by Active Resource much the same way Active Record maps
|
22
|
+
model classes to database tables. When a request is made to a remote resource, a REST JSON request is
|
23
|
+
generated, transmitted, and the result received and serialized into a usable Ruby object.
|
24
|
+
|
25
|
+
### Configuration and Usage
|
26
|
+
|
27
|
+
Putting Active Resource to use is very similar to Active Record. It's as simple as creating a model class
|
28
|
+
that inherits from `ActiveResource::Base` and providing a `site` class variable to it:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class Person < ActiveResource::Base
|
32
|
+
self.site = "http://api.people.com:3000"
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Now the Person class is REST enabled and can invoke REST services very similarly to how Active Record invokes
|
37
|
+
life cycle methods that operate against a persistent store.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
# Find a person with id = 1
|
41
|
+
tyler = Person.find(1)
|
42
|
+
Person.exists?(1) # => true
|
43
|
+
```
|
44
|
+
|
45
|
+
As you can see, the methods are quite similar to Active Record's methods for dealing with database
|
46
|
+
records. But rather than dealing directly with a database record, you're dealing with HTTP resources
|
47
|
+
(which may or may not be database records).
|
48
|
+
|
49
|
+
Connection settings (`site`, `headers`, `user`, `password`, `bearer_token`, `proxy`) and the connections
|
50
|
+
themselves are store in thread-local variables to make them thread-safe, so you can also set these
|
51
|
+
dynamically, even in a multi-threaded environment, for instance:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
ActiveResource::Base.site = api_site_for(request)
|
55
|
+
```
|
56
|
+
### Authentication
|
57
|
+
|
58
|
+
Active Resource supports the token based authentication provided by Rails through the
|
59
|
+
`ActionController::HttpAuthentication::Token` class using custom headers.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class Person < ActiveResource::Base
|
63
|
+
self.headers['Authorization'] = 'Token token="abcd"'
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
You can also set any specific HTTP header using the same way. As mentioned above, headers are
|
68
|
+
thread-safe, so you can set headers dynamically, even in a multi-threaded environment:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
ActiveResource::Base.headers['Authorization'] = current_session_api_token
|
72
|
+
```
|
73
|
+
|
74
|
+
Active Resource supports 2 options for HTTP authentication today.
|
75
|
+
|
76
|
+
1. Basic
|
77
|
+
```ruby
|
78
|
+
class Person < ActiveResource::Base
|
79
|
+
self.user = 'my@email.com'
|
80
|
+
self.password = '123'
|
81
|
+
end
|
82
|
+
# username: my@email.com password: 123
|
83
|
+
```
|
84
|
+
|
85
|
+
2. Bearer Token
|
86
|
+
```ruby
|
87
|
+
class Person < ActiveResource::Base
|
88
|
+
self.auth_type = :bearer
|
89
|
+
self.bearer_token = 'my-token123'
|
90
|
+
end
|
91
|
+
# Bearer my-token123
|
92
|
+
```
|
93
|
+
|
94
|
+
### Protocol
|
95
|
+
|
96
|
+
Active Resource is built on a standard JSON or XML format for requesting and submitting resources
|
97
|
+
over HTTP. It mirrors the RESTful routing built into Action Controller but will also work with any
|
98
|
+
other REST service that properly implements the protocol. REST uses HTTP, but unlike "typical" web
|
99
|
+
applications, it makes use of all the verbs available in the HTTP specification:
|
100
|
+
|
101
|
+
* GET requests are used for finding and retrieving resources.
|
102
|
+
* POST requests are used to create new resources.
|
103
|
+
* PUT requests are used to update existing resources.
|
104
|
+
* DELETE requests are used to delete resources.
|
105
|
+
|
106
|
+
For more information on how this protocol works with Active Resource, see the `ActiveResource::Base` documentation;
|
107
|
+
for more general information on REST web services, see the article
|
108
|
+
[here](http://en.wikipedia.org/wiki/Representational_State_Transfer).
|
109
|
+
|
110
|
+
### Find
|
111
|
+
|
112
|
+
Find requests use the GET method and expect the JSON form of whatever resource/resources is/are
|
113
|
+
being requested. So, for a request for a single element, the JSON of that item is expected in
|
114
|
+
response:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
# Expects a response of
|
118
|
+
#
|
119
|
+
# {"id":1,"first":"Tyler","last":"Durden"}
|
120
|
+
#
|
121
|
+
# for GET http://api.people.com:3000/people/1.json
|
122
|
+
#
|
123
|
+
tyler = Person.find(1)
|
124
|
+
```
|
125
|
+
|
126
|
+
The JSON document that is received is used to build a new object of type Person, with each
|
127
|
+
JSON element becoming an attribute on the object.
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
tyler.is_a? Person # => true
|
131
|
+
tyler.last # => 'Durden'
|
132
|
+
```
|
133
|
+
|
134
|
+
Any complex element (one that contains other elements) becomes its own object:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
# With this response:
|
138
|
+
# {"id":1,"first":"Tyler","address":{"street":"Paper St.","state":"CA"}}
|
139
|
+
#
|
140
|
+
# for GET http://api.people.com:3000/people/1.json
|
141
|
+
#
|
142
|
+
tyler = Person.find(1)
|
143
|
+
tyler.address # => <Person::Address::xxxxx>
|
144
|
+
tyler.address.street # => 'Paper St.'
|
145
|
+
```
|
146
|
+
|
147
|
+
Collections can also be requested in a similar fashion
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
# Expects a response of
|
151
|
+
#
|
152
|
+
# [
|
153
|
+
# {"id":1,"first":"Tyler","last":"Durden"},
|
154
|
+
# {"id":2,"first":"Tony","last":"Stark",}
|
155
|
+
# ]
|
156
|
+
#
|
157
|
+
# for GET http://api.people.com:3000/people.json
|
158
|
+
#
|
159
|
+
people = Person.all
|
160
|
+
people.first # => <Person::xxx 'first' => 'Tyler' ...>
|
161
|
+
people.last # => <Person::xxx 'first' => 'Tony' ...>
|
162
|
+
```
|
163
|
+
|
164
|
+
### Create
|
165
|
+
|
166
|
+
Creating a new resource submits the JSON form of the resource as the body of the request and expects
|
167
|
+
a 'Location' header in the response with the RESTful URL location of the newly created resource. The
|
168
|
+
id of the newly created resource is parsed out of the Location response header and automatically set
|
169
|
+
as the id of the ARes object.
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
# {"first":"Tyler","last":"Durden"}
|
173
|
+
#
|
174
|
+
# is submitted as the body on
|
175
|
+
#
|
176
|
+
# if include_root_in_json is not set or set to false => {"first":"Tyler"}
|
177
|
+
# if include_root_in_json is set to true => {"person":{"first":"Tyler"}}
|
178
|
+
#
|
179
|
+
# POST http://api.people.com:3000/people.json
|
180
|
+
#
|
181
|
+
# when save is called on a new Person object. An empty response is
|
182
|
+
# is expected with a 'Location' header value:
|
183
|
+
#
|
184
|
+
# Response (201): Location: http://api.people.com:3000/people/2
|
185
|
+
#
|
186
|
+
tyler = Person.new(:first => 'Tyler')
|
187
|
+
tyler.new? # => true
|
188
|
+
tyler.save # => true
|
189
|
+
tyler.new? # => false
|
190
|
+
tyler.id # => 2
|
191
|
+
```
|
192
|
+
|
193
|
+
### Update
|
194
|
+
|
195
|
+
'save' is also used to update an existing resource and follows the same protocol as creating a resource
|
196
|
+
with the exception that no response headers are needed -- just an empty response when the update on the
|
197
|
+
server side was successful.
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
# {"first":"Tyler"}
|
201
|
+
#
|
202
|
+
# is submitted as the body on
|
203
|
+
#
|
204
|
+
# if include_root_in_json is not set or set to false => {"first":"Tyler"}
|
205
|
+
# if include_root_in_json is set to true => {"person":{"first":"Tyler"}}
|
206
|
+
#
|
207
|
+
# PUT http://api.people.com:3000/people/1.json
|
208
|
+
#
|
209
|
+
# when save is called on an existing Person object. An empty response is
|
210
|
+
# is expected with code (204)
|
211
|
+
#
|
212
|
+
tyler = Person.find(1)
|
213
|
+
tyler.first # => 'Tyler'
|
214
|
+
tyler.first = 'Tyson'
|
215
|
+
tyler.save # => true
|
216
|
+
```
|
217
|
+
|
218
|
+
### Delete
|
219
|
+
|
220
|
+
Destruction of a resource can be invoked as a class and instance method of the resource.
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
# A request is made to
|
224
|
+
#
|
225
|
+
# DELETE http://api.people.com:3000/people/1.json
|
226
|
+
#
|
227
|
+
# for both of these forms. An empty response with
|
228
|
+
# is expected with response code (200)
|
229
|
+
#
|
230
|
+
tyler = Person.find(1)
|
231
|
+
tyler.destroy # => true
|
232
|
+
tyler.exists? # => false
|
233
|
+
Person.delete(2) # => true
|
234
|
+
Person.exists?(2) # => false
|
235
|
+
```
|
236
|
+
|
237
|
+
### Associations
|
238
|
+
|
239
|
+
Relationships between resources can be declared using the standard association syntax
|
240
|
+
that should be familiar to anyone who uses Active Record. For example, using the
|
241
|
+
class definition below:
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
class Post < ActiveResource::Base
|
245
|
+
self.site = "http://blog.io"
|
246
|
+
has_many :comments
|
247
|
+
end
|
248
|
+
|
249
|
+
post = Post.find(1) # issues GET http://blog.io/posts/1.json
|
250
|
+
comments = post.comments # issues GET http://blog.io/comments.json?post_id=1
|
251
|
+
```
|
252
|
+
|
253
|
+
In this case, the `Comment` model would have to be implemented as Active Resource, too.
|
254
|
+
|
255
|
+
If you control the server, you may wish to include nested resources thus avoiding a
|
256
|
+
second network request. Given the resource above, if the response includes comments
|
257
|
+
in the response, they will be automatically loaded into the Active Resource object.
|
258
|
+
The server-side model can be adjusted as follows to include comments in the response.
|
259
|
+
|
260
|
+
```ruby
|
261
|
+
class Post < ActiveRecord::Base
|
262
|
+
has_many :comments
|
263
|
+
|
264
|
+
def as_json(options)
|
265
|
+
super.merge(:include=>[:comments])
|
266
|
+
end
|
267
|
+
end
|
268
|
+
```
|
269
|
+
|
270
|
+
### Logging
|
271
|
+
|
272
|
+
Active Resource instruments the event `request.active_resource` when doing a request
|
273
|
+
to the remote service. You can subscribe to it by doing:
|
274
|
+
|
275
|
+
```ruby
|
276
|
+
ActiveSupport::Notifications.subscribe('request.active_resource') do |name, start, finish, id, payload|
|
277
|
+
```
|
278
|
+
|
279
|
+
The `payload` is a `Hash` with the following keys:
|
280
|
+
|
281
|
+
* `method` as a `Symbol`
|
282
|
+
* `request_uri` as a `String`
|
283
|
+
* `result` as an `Net::HTTPResponse`
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "active_resource"
|
5
|
+
require "benchmark"
|
6
|
+
|
7
|
+
TIMES = (ENV["N"] || 10_000).to_i
|
8
|
+
|
9
|
+
# deep nested resource
|
10
|
+
attrs = {
|
11
|
+
id: 1,
|
12
|
+
name: "Luis",
|
13
|
+
age: 21,
|
14
|
+
friends: [
|
15
|
+
{
|
16
|
+
name: "JK",
|
17
|
+
age: 24,
|
18
|
+
colors: ["red", "green", "blue"],
|
19
|
+
brothers: [
|
20
|
+
{
|
21
|
+
name: "Mateo",
|
22
|
+
age: 35,
|
23
|
+
children: [{ name: "Edith", age: 5 }, { name: "Martha", age: 4 }]
|
24
|
+
},
|
25
|
+
{
|
26
|
+
name: "Felipe",
|
27
|
+
age: 33,
|
28
|
+
children: [{ name: "Bryan", age: 1 }, { name: "Luke", age: 0 }]
|
29
|
+
}
|
30
|
+
]
|
31
|
+
},
|
32
|
+
{
|
33
|
+
name: "Eduardo",
|
34
|
+
age: 20,
|
35
|
+
colors: [],
|
36
|
+
brothers: [
|
37
|
+
{
|
38
|
+
name: "Sebas",
|
39
|
+
age: 23,
|
40
|
+
children: [{ name: "Andres", age: 0 }, { name: "Jorge", age: 2 }]
|
41
|
+
},
|
42
|
+
{
|
43
|
+
name: "Elsa",
|
44
|
+
age: 19,
|
45
|
+
children: [{ name: "Natacha", age: 1 }]
|
46
|
+
},
|
47
|
+
{
|
48
|
+
name: "Milena",
|
49
|
+
age: 16,
|
50
|
+
children: []
|
51
|
+
}
|
52
|
+
]
|
53
|
+
}
|
54
|
+
]
|
55
|
+
}
|
56
|
+
|
57
|
+
class Customer < ActiveResource::Base
|
58
|
+
self.site = "http://37s.sunrise.i:3000"
|
59
|
+
end
|
60
|
+
|
61
|
+
module Nested
|
62
|
+
class Customer < ActiveResource::Base
|
63
|
+
self.site = "http://37s.sunrise.i:3000"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
Benchmark.bm(40) do |x|
|
68
|
+
x.report("Model.new (instantiation)") { TIMES.times { Customer.new } }
|
69
|
+
x.report("Nested::Model.new (instantiation)") { TIMES.times { Nested::Customer.new } }
|
70
|
+
x.report("Model.new (setting attributes)") { TIMES.times { Customer.new attrs } }
|
71
|
+
x.report("Nested::Model.new (setting attributes)") { TIMES.times { Nested::Customer.new attrs } }
|
72
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveResource
|
4
|
+
class ActiveJobSerializer < ActiveJob::Serializers::ObjectSerializer
|
5
|
+
def serialize(resource)
|
6
|
+
super(
|
7
|
+
"class" => resource.class.name,
|
8
|
+
"persisted" => resource.persisted?,
|
9
|
+
"prefix_options" => resource.prefix_options.as_json,
|
10
|
+
"attributes" => resource.attributes.as_json
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
def deserialize(hash)
|
15
|
+
hash["class"].constantize.new(hash["attributes"]).tap do |resource|
|
16
|
+
resource.persisted = hash["persisted"]
|
17
|
+
resource.prefix_options = hash["prefix_options"]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def klass
|
23
|
+
ActiveResource::Base
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveResource::Associations::Builder
|
4
|
+
class Association # :nodoc:
|
5
|
+
# providing a Class-Variable, which will have a different store of subclasses
|
6
|
+
class_attribute :valid_options
|
7
|
+
self.valid_options = [:class_name]
|
8
|
+
|
9
|
+
# would identify subclasses of association
|
10
|
+
class_attribute :macro
|
11
|
+
|
12
|
+
attr_reader :model, :name, :options, :klass
|
13
|
+
|
14
|
+
def self.build(model, name, options)
|
15
|
+
new(model, name, options).build
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(model, name, options)
|
19
|
+
@model, @name, @options = model, name, options
|
20
|
+
end
|
21
|
+
|
22
|
+
def build
|
23
|
+
validate_options
|
24
|
+
model.create_reflection(self.class.macro, name, options)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def validate_options
|
29
|
+
options.assert_valid_keys(self.class.valid_options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveResource::Associations::Builder
|
4
|
+
class BelongsTo < Association
|
5
|
+
self.valid_options += [:foreign_key]
|
6
|
+
|
7
|
+
self.macro = :belongs_to
|
8
|
+
|
9
|
+
def build
|
10
|
+
validate_options
|
11
|
+
reflection = model.create_reflection(self.class.macro, name, options)
|
12
|
+
model.defines_belongs_to_finder_method(reflection)
|
13
|
+
reflection
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveResource::Associations::Builder
|
4
|
+
class HasMany < Association
|
5
|
+
self.macro = :has_many
|
6
|
+
|
7
|
+
def build
|
8
|
+
validate_options
|
9
|
+
model.create_reflection(self.class.macro, name, options).tap do |reflection|
|
10
|
+
model.defines_has_many_finder_method(reflection)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveResource::Associations::Builder
|
4
|
+
class HasOne < Association
|
5
|
+
self.macro = :has_one
|
6
|
+
|
7
|
+
def build
|
8
|
+
validate_options
|
9
|
+
model.create_reflection(self.class.macro, name, options).tap do |reflection|
|
10
|
+
model.defines_has_one_finder_method(reflection)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveResource::Associations
|
4
|
+
module Builder
|
5
|
+
autoload :Association, "active_resource/associations/builder/association"
|
6
|
+
autoload :HasMany, "active_resource/associations/builder/has_many"
|
7
|
+
autoload :HasOne, "active_resource/associations/builder/has_one"
|
8
|
+
autoload :BelongsTo, "active_resource/associations/builder/belongs_to"
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
# Specifies a one-to-many association.
|
14
|
+
#
|
15
|
+
# === Options
|
16
|
+
# [:class_name]
|
17
|
+
# Specify the class name of the association. This class name would
|
18
|
+
# be used for resolving the association class.
|
19
|
+
#
|
20
|
+
# ==== Example for [:class_name] - option
|
21
|
+
# GET /posts/123.json delivers following response body:
|
22
|
+
# {
|
23
|
+
# title: "ActiveResource now has associations",
|
24
|
+
# body: "Lorem Ipsum"
|
25
|
+
# comments: [
|
26
|
+
# {
|
27
|
+
# content: "..."
|
28
|
+
# },
|
29
|
+
# {
|
30
|
+
# content: "..."
|
31
|
+
# }
|
32
|
+
# ]
|
33
|
+
# }
|
34
|
+
# ====
|
35
|
+
#
|
36
|
+
# <tt>has_many :comments, :class_name => 'myblog/comment'</tt>
|
37
|
+
# Would resolve those comments into the <tt>Myblog::Comment</tt> class.
|
38
|
+
#
|
39
|
+
# If the response body does not contain an attribute matching the association name
|
40
|
+
# a request sent to the index action under the current resource.
|
41
|
+
# For the example above, if the comments are not present the requested path would be:
|
42
|
+
# GET /posts/123/comments.xml
|
43
|
+
def has_many(name, options = {})
|
44
|
+
Builder::HasMany.build(self, name, options)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Specifies a one-to-one association.
|
48
|
+
#
|
49
|
+
# === Options
|
50
|
+
# [:class_name]
|
51
|
+
# Specify the class name of the association. This class name would
|
52
|
+
# be used for resolving the association class.
|
53
|
+
#
|
54
|
+
# ==== Example for [:class_name] - option
|
55
|
+
# GET /posts/1.json delivers following response body:
|
56
|
+
# {
|
57
|
+
# title: "ActiveResource now has associations",
|
58
|
+
# body: "Lorem Ipsum",
|
59
|
+
# author: {
|
60
|
+
# name: "Gabby Blogger",
|
61
|
+
# }
|
62
|
+
# }
|
63
|
+
# ====
|
64
|
+
#
|
65
|
+
# <tt>has_one :author, :class_name => 'myblog/author'</tt>
|
66
|
+
# Would resolve this author into the <tt>Myblog::Author</tt> class.
|
67
|
+
#
|
68
|
+
# If the response body does not contain an attribute matching the association name
|
69
|
+
# a request is sent to a singleton path under the current resource.
|
70
|
+
# For example, if a Product class <tt>has_one :inventory</tt> calling <tt>Product#inventory</tt>
|
71
|
+
# will generate a request on /products/:product_id/inventory.json.
|
72
|
+
#
|
73
|
+
def has_one(name, options = {})
|
74
|
+
Builder::HasOne.build(self, name, options)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Specifies a one-to-one association with another class. This class should only be used
|
78
|
+
# if this class contains the foreign key.
|
79
|
+
#
|
80
|
+
# Methods will be added for retrieval and query for a single associated object, for which
|
81
|
+
# this object holds an id:
|
82
|
+
#
|
83
|
+
# [association(force_reload = false)]
|
84
|
+
# Returns the associated object. +nil+ is returned if the foreign key is +nil+.
|
85
|
+
# Throws a ActiveResource::ResourceNotFound exception if the foreign key is not +nil+
|
86
|
+
# and the resource is not found.
|
87
|
+
#
|
88
|
+
# (+association+ is replaced with the symbol passed as the first argument, so
|
89
|
+
# <tt>belongs_to :post</tt> would add among others <tt>post.nil?</tt>.
|
90
|
+
#
|
91
|
+
# === Example
|
92
|
+
#
|
93
|
+
# A Comment class declares <tt>belongs_to :post</tt>, which will add:
|
94
|
+
# * <tt>Comment#post</tt> (similar to <tt>Post.find(post_id)</tt>)
|
95
|
+
# The declaration can also include an options hash to specialize the behavior of the association.
|
96
|
+
#
|
97
|
+
# === Options
|
98
|
+
# [:class_name]
|
99
|
+
# Specify the class name for the association. Use it only if that name can't be inferred from association name.
|
100
|
+
# So <tt>belongs_to :post</tt> will by default be linked to the Post class, but if the real class name is Article,
|
101
|
+
# you'll have to specify it with this option.
|
102
|
+
# [:foreign_key]
|
103
|
+
# Specify the foreign key used for the association. By default this is guessed to be the name
|
104
|
+
# of the association with an "_id" suffix. So a class that defines a <tt>belongs_to :post</tt>
|
105
|
+
# association will use "post_id" as the default <tt>:foreign_key</tt>. Similarly,
|
106
|
+
# <tt>belongs_to :article, :class_name => "Post"</tt> will use a foreign key
|
107
|
+
# of "article_id".
|
108
|
+
#
|
109
|
+
# Option examples:
|
110
|
+
# <tt>belongs_to :customer, :class_name => 'User'</tt>
|
111
|
+
# Creates a belongs_to association called customer which is represented through the <tt>User</tt> class.
|
112
|
+
#
|
113
|
+
# <tt>belongs_to :customer, :foreign_key => 'user_id'</tt>
|
114
|
+
# Creates a belongs_to association called customer which would be resolved by the foreign_key <tt>user_id</tt> instead of <tt>customer_id</tt>
|
115
|
+
#
|
116
|
+
def belongs_to(name, options = {})
|
117
|
+
Builder::BelongsTo.build(self, name, options)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Defines the belongs_to association finder method
|
121
|
+
def defines_belongs_to_finder_method(reflection)
|
122
|
+
method_name = reflection.name
|
123
|
+
ivar_name = :"@#{method_name}"
|
124
|
+
|
125
|
+
if method_defined?(method_name)
|
126
|
+
instance_variable_set(ivar_name, nil)
|
127
|
+
remove_method(method_name)
|
128
|
+
end
|
129
|
+
|
130
|
+
define_method(method_name) do
|
131
|
+
if instance_variable_defined?(ivar_name)
|
132
|
+
instance_variable_get(ivar_name)
|
133
|
+
elsif attributes.include?(method_name)
|
134
|
+
attributes[method_name]
|
135
|
+
elsif association_id = send(reflection.foreign_key)
|
136
|
+
instance_variable_set(ivar_name, reflection.klass.find(association_id))
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def defines_has_many_finder_method(reflection)
|
142
|
+
method_name = reflection.name
|
143
|
+
ivar_name = :"@#{method_name}"
|
144
|
+
|
145
|
+
define_method(method_name) do
|
146
|
+
if instance_variable_defined?(ivar_name)
|
147
|
+
instance_variable_get(ivar_name)
|
148
|
+
elsif attributes.include?(method_name)
|
149
|
+
attributes[method_name]
|
150
|
+
elsif !new_record?
|
151
|
+
instance_variable_set(ivar_name, reflection.klass.find(:all, params: { "#{self.class.element_name}_id": self.id }))
|
152
|
+
else
|
153
|
+
instance_variable_set(ivar_name, reflection.klass.find(:all))
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# Defines the has_one association
|
159
|
+
def defines_has_one_finder_method(reflection)
|
160
|
+
method_name = reflection.name
|
161
|
+
ivar_name = :"@#{method_name}"
|
162
|
+
|
163
|
+
define_method(method_name) do
|
164
|
+
if instance_variable_defined?(ivar_name)
|
165
|
+
instance_variable_get(ivar_name)
|
166
|
+
elsif attributes.include?(method_name)
|
167
|
+
attributes[method_name]
|
168
|
+
elsif reflection.klass.respond_to?(:singleton_name)
|
169
|
+
instance_variable_set(ivar_name, reflection.klass.find(params: { "#{self.class.element_name}_id": self.id }))
|
170
|
+
else
|
171
|
+
instance_variable_set(ivar_name, reflection.klass.find(:one, from: "/#{self.class.collection_name}/#{self.id}/#{method_name}#{self.class.format_extension}"))
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|