Module: ActiveScaffold::Bridges::ActiveStorage::ActiveStorageBridgeHelpers

Defined in:
lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.active_storage_has_many_fields(klass) ⇒ Object

has_many :“#name_attachments”, -> { where(name: name) }, as: :record, class_name: “ActiveStorage::Attachment”, inverse_of: :record, dependent: false do


15
16
17
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 15

def active_storage_has_many_fields(klass)
  klass.reflect_on_all_associations(:has_many)&.select { |reflection| reflection.class_name == 'ActiveStorage::Attachment' } &.collect { |association| association.name[0..-13] } || []
end

.active_storage_has_one_fields(klass) ⇒ Object

has_one :“#name_attachment”, -> { where(name: name) }, class_name: “ActiveStorage::Attachment”, as: :record, inverse_of: :record, dependent: false


10
11
12
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 10

def active_storage_has_one_fields(klass)
  klass.reflect_on_all_associations(:has_one)&.select { |reflection| reflection.class_name == 'ActiveStorage::Attachment' } &.collect { |association| association.name[0..-12] } || []
end

.generate_delete_helpers(klass) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 23

def generate_delete_helpers(klass)
  (active_storage_has_one_fields(klass) | active_storage_has_many_fields(klass)).each do |field|
    klass.send :class_eval, <<-CODE, __FILE__, __LINE__ + 1 unless klass.method_defined?(:"#{field}_with_delete=")
      attr_reader :delete_#{field}

      def delete_#{field}=(value)
        value = (value=="true") if String===value
        return unless value

        # passing nil to the file column causes the file to be deleted.
        self.#{field}.purge
      end
    CODE
  end
end

.klass_has_active_storage_fields?(klass) ⇒ Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 19

def klass_has_active_storage_fields?(klass)
  active_storage_has_one_fields(klass).present? || active_storage_has_many_fields(klass).present?
end

Instance Method Details

#active_storage_has_many_fieldsObject


44
45
46
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 44

def active_storage_has_many_fields
  @active_storage_has_many_fields ||= ActiveStorageBridgeHelpers.active_storage_has_many_fields(self)
end

#active_storage_has_one_fieldsObject


40
41
42
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 40

def active_storage_has_one_fields
  @active_storage_has_one_fields ||= ActiveStorageBridgeHelpers.active_storage_has_one_fields(self)
end

#generate_delete_helpersObject


48
49
50
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 48

def generate_delete_helpers
  ActiveStorageBridgeHelpers.generate_delete_helpers(self)
end