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



12
13
14
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 12

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



7
8
9
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 7

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



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

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)


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

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



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

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

#active_storage_has_one_fieldsObject



37
38
39
# File 'lib/active_scaffold/bridges/active_storage/active_storage_helpers.rb', line 37

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

#generate_delete_helpersObject



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

def generate_delete_helpers
  ActiveStorageBridgeHelpers.generate_delete_helpers(self)
end