diff options
author | Josh Susser <josh@hasmanythrough.com> | 2018-05-17 14:22:00 -0700 |
---|---|---|
committer | Josh Susser <josh@hasmanythrough.com> | 2018-05-17 14:51:15 -0700 |
commit | fd0bd1bf682622f064ac437ceee4e1b2a6b6d3b9 (patch) | |
tree | a3c0cdc07acdae23fee748605c8b19a6a9f81462 /activestorage/test | |
parent | 6c05728a507fe828647c2a0f62cf8ede1534a642 (diff) | |
download | rails-fd0bd1bf682622f064ac437ceee4e1b2a6b6d3b9.tar.gz rails-fd0bd1bf682622f064ac437ceee4e1b2a6b6d3b9.tar.bz2 rails-fd0bd1bf682622f064ac437ceee4e1b2a6b6d3b9.zip |
Generate getter and setter methods in mixin
Generated attachment getter and setter methods are created within
the model's `GeneratedAssociationMethods` module to allow overriding
and composition using `super`.
Includes tests for new functionality.
Co-authored-by: Josh Susser <josh@hasmanythrough.com>
Co-authored-by: Jamon Douglas <terrildouglas@gmail.com>
Diffstat (limited to 'activestorage/test')
-rw-r--r-- | activestorage/test/models/attached_test.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/activestorage/test/models/attached_test.rb b/activestorage/test/models/attached_test.rb new file mode 100644 index 0000000000..14395e12df --- /dev/null +++ b/activestorage/test/models/attached_test.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require "test_helper" +require "database/setup" + +class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase + include ActiveJob::TestHelper + + setup do + @user = User.create!(name: "Josh") + end + + teardown { ActiveStorage::Blob.all.each(&:purge) } + + test "overriding has_one_attached methods works" do + # attach blob before messing with getter, which breaks `#attach` + @user.avatar.attach create_blob(filename: "funky.jpg") + + # inherited only + assert_equal "funky.jpg", @user.avatar.filename.to_s + + User.class_eval do + def avatar + super.filename.to_s.reverse + end + end + + # override with super + assert_equal "funky.jpg".reverse, @user.avatar + + User.send(:remove_method, :avatar) + end + + test "overriding has_many_attached methods works" do + # attach blobs before messing with getter, which breaks `#attach` + @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg") + + # inherited only + assert_equal "funky.jpg", @user.highlights.first.filename.to_s + assert_equal "wonky.jpg", @user.highlights.second.filename.to_s + + User.class_eval do + def highlights + super.reverse + end + end + + # override with super + assert_equal "wonky.jpg", @user.highlights.first.filename.to_s + assert_equal "funky.jpg", @user.highlights.second.filename.to_s + + User.send(:remove_method, :highlights) + end +end |