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



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

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
10
11
# 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



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

def generate_delete_helpers(klass)
  (active_storage_has_one_fields(klass) | active_storage_has_many_fields(klass)).each do |field|
    klass.send :class_eval, "      attr_reader :delete_\#{field}\n\n      def delete_\#{field}=(value)\n        value = (value==\"true\") if String===value\n        return unless value\n\n        # passing nil to the file column causes the file to be deleted.\n        self.\#{field}.purge\n      end\n    CODE\n  end\nend\n", __FILE__, __LINE__ + 1 unless klass.method_defined?(:"#{field}_with_delete=")

.klass_has_active_storage_fields?(klass) ⇒ Boolean



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

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



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

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

#active_storage_has_one_fieldsObject



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

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

#generate_delete_helpersObject



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

def generate_delete_helpers
  ActiveStorageBridgeHelpers.generate_delete_helpers(self)
end