Module: Cardio

Extended by:
Schema, Utils
Defined in:
lib/cardio.rb,
lib/cardio/utils.rb,
lib/cardio/schema.rb

Defined Under Namespace

Modules: Schema, Utils

Constant Summary collapse

CARD_GEM_ROOT =
File.expand_path("..", __dir__)

Class Method Summary collapse

Methods included from Schema

assume_migrated_upto_version, deck_migration?, migrate, migration_context, schema, schema_mode, schema_stamp_path, schema_suffix, with_migration_table, with_suffix

Methods included from Utils

delete_tmp_files!, seed_test_db

Class Method Details

.add_db_pathsObject



173
174
175
176
177
178
179
180
# File 'lib/cardio.rb', line 173

def add_db_paths
  add_path "db"
  add_path "db/migrate"
  add_path "db/migrate_core_cards"
  add_path "db/migrate_deck", root: root, with: "db/migrate"
  add_path "db/migrate_deck_cards", root: root, with: "db/migrate_cards"
  add_path "db/seeds.rb", with: "db/seeds.rb"
end

.add_initializer_pathsObject



182
183
184
185
186
187
188
# File 'lib/cardio.rb', line 182

def add_initializer_paths
  add_path "config/initializers", glob: "**/*.rb"
  add_initializers root
  each_mod_path do |mod_path|
    add_initializers mod_path, false, "core_initializers"
  end
end

.add_initializers(base_dir, mod = false, init_dir = "initializers") ⇒ Object



197
198
199
200
201
202
# File 'lib/cardio.rb', line 197

def add_initializers base_dir, mod=false, init_dir="initializers"
  Dir.glob("#{base_dir}/config/#{init_dir}").each do |initializers_dir|
    path_mark = mod ? "mod/config/initializers" : "config/initializers"
    paths[path_mark] << initializers_dir
  end
end

.add_lib_dirs_to_autoload_paths(config) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cardio.rb', line 113

def add_lib_dirs_to_autoload_paths config
  config.autoload_paths += Dir["#{gem_root}/lib"]
  config.autoload_paths += Dir["#{gem_root}/mod/*/lib"]
  config.watchable_dirs["#{gem_root}/mod/*/set"] = [:rb]

  config.autoload_paths += Dir["#{root}/mod/*/lib"]
  config.watchable_dirs["#{root}/mod/*/set"] = [:rb]
  gem_mod_paths.each do |_mod_name, mod_path|
    config.autoload_paths += Dir["#{mod_path}/lib"]
    config.watchable_dirs["#{mod_path}/set"] = [:rb]
  end
  # the watachable_dirs are processes in
  # set_clear_dependencies_hook hook in the railties gem in finisher.rb

  # TODO: move this to the right place in decko
  config.autoload_paths += Dir["#{Decko.gem_root}/lib"]
  # config.autoload_paths += Dir["#{gem_root}/lib/**/"]
  # config.autoload_paths += Dir["#{gem_root}/mod/*/lib/**/"]
  # config.autoload_paths += Dir["#{root}/mod/*/lib/**/"]
  # gem_mod_paths.each do |_mod_name, mod_path|
  #  config.autoload_paths += Dir["#{mod_path}/lib/**/"]
  # end
end

.add_mod_initializer_pathsObject



190
191
192
193
194
195
# File 'lib/cardio.rb', line 190

def add_mod_initializer_paths
  add_path "mod/config/initializers", glob: "**/*.rb"
  each_mod_path do |mod_path|
    add_initializers mod_path, true
  end
end

.add_path(path, options = {}) ⇒ Object



223
224
225
226
227
# File 'lib/cardio.rb', line 223

def add_path path, options={}
  root = options.delete(:root) || gem_root
  options[:with] = File.join(root, (options[:with] || path))
  paths.add path, options
end

.cacheObject



36
37
38
# File 'lib/cardio.rb', line 36

def cache
  @cache ||= ::Rails.cache
end

.card_defined?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/cardio.rb', line 21

def card_defined?
  const_defined? "Card"
end

.default_configsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cardio.rb', line 40

def default_configs
  {
    read_only: read_only?,

    # if you disable inline styles tinymce's formatting options stop working
    allow_inline_styles: true,

    recaptcha_public_key: nil, # deprecated; use recaptcha_site_key instead
    recaptcha_private_key: nil, # deprecated; use recaptcha_secret_key instead
    recaptcha_proxy: nil,
    recaptcha_site_key: nil,
    recaptcha_secret_key: nil,
    recaptcha_minimum_score: 0.5,

    override_host: nil,
    override_protocol: nil,

    no_authentication: false,
    files_web_path: "files",

    max_char_count: 200,
    max_depth: 20,
    email_defaults: nil,

    token_expiry: 2.days,
    acts_per_page: 10,
    space_last_in_multispace: true,
    closed_search_limit: 10,
    paging_limit: 20,

    non_createable_types: [%w[signup setting set session bootswatch_skin customized_bootswatch_skin]], # FIXME
    view_cache: false,
    rss_enabled: false,
    double_click: :signed_in,

    encoding: "utf-8",
    request_logger: false,
    performance_logger: false,
    sql_comments: true,

    file_storage: :local,
    file_buckets: {},
    file_default_bucket: nil,
    protocol_and_host: nil,

    rich_text_editor: :tinymce,

    persistent_cache: true,
    prepopulate_cache: false,
    machine_refresh: :cautious, # options: eager, cautious, never
    compress_javascript: true,

    allow_irreversible_admin_tasks: false,
    raise_all_rendering_errors: false,
    rescue_all_in_controller: true,
    navbox_match_start_only: true,

    reload_sets: false,
    load_strategy: :eval
  }
end

.each_mod_pathObject



204
205
206
207
208
209
210
211
212
213
# File 'lib/cardio.rb', line 204

def each_mod_path
  paths["mod"].each do |mods_path|
    Dir.glob("#{mods_path}/*").each do |single_mod_path|
      yield single_mod_path
    end
  end
  gem_mod_paths.each do |_mod_name, mod_path|
    yield mod_path
  end
end

.future_stampObject



229
230
231
232
# File 'lib/cardio.rb', line 229

def future_stamp
  # # used in test data
  @future_stamp ||= Time.zone.local 2020, 1, 1, 0, 0, 0
end

.gem_mod_pathsObject

Returns Hash with key mod names (without card-mod prefix) and values the full path to the mod.

Returns:

  • Hash with key mod names (without card-mod prefix) and values the full path to the mod



139
140
141
142
143
144
145
146
147
# File 'lib/cardio.rb', line 139

def gem_mod_paths
  @gem_mods ||=
    Bundler.definition.specs.each_with_object({}) do |gem_spec, h|
      mod_name = mod_name_from_gem_spec gem_spec
      next unless mod_name

      h[mod_name] = gem_spec.full_gem_path
    end
end

.gem_rootObject



219
220
221
# File 'lib/cardio.rb', line 219

def gem_root
  CARD_GEM_ROOT
end

.load_card!Object



31
32
33
34
# File 'lib/cardio.rb', line 31

def load_card!
  require "card"
  ActiveSupport.run_load_hooks :after_card
end

.load_card?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/cardio.rb', line 25

def load_card?
  ActiveRecord::Base.connection && !card_defined?
rescue
  false
end

.migration_paths(type) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/cardio.rb', line 234

def migration_paths type
  list = paths["db/migrate#{schema_suffix type}"].to_a
  case type
  when :core_cards
    list += mod_migration_paths "migrate_core_cards"
  when :deck_cards
    list += mod_migration_paths "migrate_cards"
  when :deck
    list += mod_migration_paths "migrate"
  end
  list.flatten
end

.mod_migration_paths(dir) ⇒ Object



247
248
249
250
251
# File 'lib/cardio.rb', line 247

def mod_migration_paths dir
  [].tap do |list|
    Card::Mod.dirs.each("db/#{dir}") { |path| list.concat Dir.glob path }
  end
end

.read_only?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/cardio.rb', line 149

def read_only?
  !ENV["DECKO_READ_ONLY"].nil?
end

.rootObject



215
216
217
# File 'lib/cardio.rb', line 215

def root
  @@config.root
end

.set_config(config) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/cardio.rb', line 102

def set_config config
  @@config = config
  config.active_job.queue_adapter = :delayed_job # better place for this?

  add_lib_dirs_to_autoload_paths config

  default_configs.each_pair do |setting, value|
    set_default_value(config, setting, *value)
  end
end

.set_default_value(config, setting, *value) ⇒ Object

In production mode set_config gets called twice. The second call overrides all deck config settings so don't change settings here if they already exist



156
157
158
# File 'lib/cardio.rb', line 156

def set_default_value config, setting, *value
  config.send("#{setting}=", *value) unless config.respond_to? setting
end

.set_paths(paths) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cardio.rb', line 160

def set_paths paths
  @@paths = paths
  add_path "tmp/set", root: root
  add_path "tmp/set_pattern", root: root

  add_path "mod"        # add card gem's mod path
  paths["mod"] << "mod" # add deck's mod path

  add_db_paths
  add_initializer_paths
  add_mod_initializer_paths
end