aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/test/models/attached_test.rb
blob: 14395e12dfa5fb5f3a6f61851eed9595f5d4991c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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