Module: Card::Set::Abstract::Attachment::Cloud

Extended by:
Card::Set
Defined in:
card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb,
card/tmpsets/set/mod013-card-mod-carrierwave/abstract/attachment/cloud.rb

Overview

Set: Abstract (Attachment, Cloud)

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



7
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/card-mod-carrierwave/set/abstract/attachment/cloud.rb"; end

Instance Method Details

#bucketObject



24
25
26
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 24

def bucket
  @bucket ||= cloud? && (new_card_bucket || bucket_from_content || bucket_from_config)
end

#bucket=(value) ⇒ Object



134
135
136
137
138
139
140
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 134

def bucket= value
  if @action == :update
    @new_bucket = value
  else
    @bucket = value
  end
end

#bucket_configObject



38
39
40
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 38

def bucket_config
  @bucket_config ||= load_bucket_config
end

#bucket_from_configObject



125
126
127
128
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 125

def bucket_from_config
  cnf = Cardio.config
  cnf.file_default_bucket || cnf.file_buckets&.keys&.first
end

#bucket_from_contentObject



120
121
122
123
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 120

def bucket_from_content
  return unless content
  content.match(/^\((?<bucket>[^)]+)\)/) { |m| m[:bucket] }
end

#cant_find_in_bucket!(need) ⇒ Object

Raises:



69
70
71
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 69

def cant_find_in_bucket! need
  raise Card::Error, "couldn't find #{need} for bucket #{bucket}"
end

#change_bucket_if_read_only?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 130

def change_bucket_if_read_only?
  cloud? && bucket_config[:read_only] && attachment_is_changing?
end

#credential_config(config) {|config[:credentials]| ... } ⇒ Object

Yields:

  • (config[:credentials])


83
84
85
86
87
88
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 83

def credential_config config
  config[:credentials] ||= {}
  yield config[:credentials]
  config.delete :credentials if config[:credentials].blank?
  config
end

#credential_from_env_regexpObject



110
111
112
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 110

def credential_from_env_regexp
  Regexp.new "^(?:#{bucket.to_s.upcase}_)?CREDENTIALS_(?<option>.+)$"
end

#each_config_option_from_envObject



90
91
92
93
94
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 90

def each_config_option_from_env
  CarrierWave::FileCardUploader::CONFIG_OPTIONS.each do |key|
    yield key unless key.in? %i[attributes credentials]
  end
end

#each_credential_from_envObject



102
103
104
105
106
107
108
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 102

def each_credential_from_env
  regexp = credential_from_env_regexp
  ENV.each_key do |env_key|
    next unless (m = regexp.match env_key)
    yield m[:option].downcase.to_sym
  end
end

#ensure_bucket_configObject



54
55
56
57
58
59
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 54

def ensure_bucket_config
  yield.tap do |config|
    require_configuration! config
    require_credentials! config
  end
end

#event: change_bucket_if_read_onlyObject



8
9
10
11
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 8

event :change_bucket_if_read_only, :initialize,
      on: :update, when: :change_bucket_if_read_only? do
  @new_storage_type = storage_type_from_config
end

#event: validate_storage_type_updateObject



13
14
15
16
17
18
19
20
21
22
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 13

event :validate_storage_type_update, :validate, on: :update, when: :cloud? do
  # FIXME: make it possible to retrieve the file from cloud storage
  #   to store it somewhere else. Currently, it only works to change the
  #   storage type if a new file is provided
  #   i.e. `update storage_type: :local` fails but
  #        `update storage_type: :local, file: [file handle]` is ok
  return unless storage_type_changed? && !attachment_is_changing?

  errors.add :storage_type, tr(:moving_files_is_not_supported)
end

#load_bucket_configObject



42
43
44
45
46
47
48
49
50
51
52
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 42

def load_bucket_config
  return {} unless bucket
  bucket_config = Cardio.config.file_buckets&.dig(bucket.to_sym) || {}
  bucket_config.symbolize_keys!
  bucket_config[:credentials]&.symbolize_keys!
  # we don't want :attributes hash symbolized, so we can't use
  # deep_symbolize_keys
  ensure_bucket_config do
    load_bucket_config_from_env bucket_config
  end
end

#load_bucket_config_from_env(config) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 73

def load_bucket_config_from_env config
  config ||= {}
  each_config_option_from_env do |key|
    replace_with_env_variable config, key
  end
  credential_config config do |cred_hash|
    load_bucket_credentials_from_env cred_hash
  end
end

#load_bucket_credentials_from_env(cred_config) ⇒ Object



96
97
98
99
100
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 96

def load_bucket_credentials_from_env cred_config
  each_credential_from_env do |option|
    replace_with_env_variable cred_config, option, "credentials"
  end
end

#new_card_bucketObject



28
29
30
31
32
33
34
35
36
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 28

def new_card_bucket
  return unless new_card?
  # If the file is assigned before the bucket option we have to
  # check if there is a bucket options in set_specific.
  # That happens for exmaple when the file appears before the bucket in the
  # options hash:
  #   Card.create file: file_handle, bucket: "my_bucket"
  set_specific[:bucket] || set_specific["bucket"] || bucket_from_config
end

#replace_with_env_variable(config, option, prefix = nil) ⇒ Object



114
115
116
117
118
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 114

def replace_with_env_variable config, option, prefix=nil
  env_key = [prefix, option].compact.join("_").upcase
  new_value = ENV["#{bucket.to_s.upcase}_#{env_key}"] || ENV[env_key]
  config[option] = new_value if new_value
end

#require_configuration!(config) ⇒ Object



61
62
63
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 61

def require_configuration! config
  cant_find_in_bucket! "configuration" unless config.present?
end

#require_credentials!(config) ⇒ Object



65
66
67
# File 'card/tmpsets/set/mod014-card-mod-carrierwave/abstract/attachment/cloud.rb', line 65

def require_credentials! config
  cant_find_in_bucket! "credentials" unless config[:credentials]
end