Module: Cardio

Defined in:
lib/cardio.rb

Constant Summary collapse

CARD_GEM_ROOT =
File.expand_path('../..', __FILE__)

Class Method Summary collapse

Class Method Details

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



120
121
122
123
124
# File 'lib/cardio.rb', line 120

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

.assume_migrated_upto_version(type) ⇒ Object



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

def assume_migrated_upto_version type
  Cardio.schema_mode(type) do
    ActiveRecord::Schema.assume_migrated_upto_version(
      Cardio.schema(type), Cardio.migration_paths(type)
    )
  end
end

.cacheObject



19
20
21
# File 'lib/cardio.rb', line 19

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

.default_configsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cardio.rb', line 23

def default_configs
  {
    read_only:              read_only?,
    allow_inline_styles:    false,

    recaptcha_public_key:   nil,
    recaptcha_private_key:  nil,
    recaptcha_proxy:        nil,

    cache_store:            [:file_store, 'tmp/cache'],
    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,
    revisions_per_page:     10,
    space_last_in_multispace: true,
    closed_search_limit:    50,

    non_createable_types:   [%w{ signup setting set }],
    view_cache:             false,

    encoding:               'utf-8',
    request_logger:         false,
    performance_logger:     false,
    sql_comments:           true
  }
end

.delete_tmp_files(id = nil) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/cardio.rb', line 157

def delete_tmp_files id=nil
  dir = Cardio.paths['files'].existent.first + '/tmp'
  dir += "/#{id}" if id
  FileUtils.rm_rf dir, secure: true
rescue
  Rails.logger.info 'failed to remove tmp files'
end

.each_mod_pathObject



106
107
108
109
110
# File 'lib/cardio.rb', line 106

def each_mod_path
  paths['mod'].each do |mod_path|
    yield mod_path
  end
end

.future_stampObject



126
127
128
129
# File 'lib/cardio.rb', line 126

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

.gem_rootObject



116
117
118
# File 'lib/cardio.rb', line 116

def gem_root
  CARD_GEM_ROOT
end

.migration_paths(type) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/cardio.rb', line 131

def migration_paths type
  list = paths["db/migrate#{schema_suffix type}"].to_a
  if type == :deck_cards
    list += Card::Loader.mod_dirs.map do |p|
      Dir.glob "#{p}/db/migrate_cards"
    end.flatten
  end
  list
end

.read_only?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/cardio.rb', line 71

def read_only?
  !ENV['WAGN_READ_ONLY'].nil?
end

.rootObject



112
113
114
# File 'lib/cardio.rb', line 112

def root
  @@config.root
end

.schema(type = nil) ⇒ Object



175
176
177
# File 'lib/cardio.rb', line 175

def schema type=nil
  File.read(schema_stamp_path type).strip
end

.schema_mode(type) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/cardio.rb', line 165

def schema_mode type
  new_suffix = Cardio.schema_suffix type
  original_suffix = ActiveRecord::Base.table_name_suffix
  ActiveRecord::Base.table_name_suffix = new_suffix
  ActiveRecord::SchemaMigration.reset_table_name
  yield
  ActiveRecord::Base.table_name_suffix = original_suffix
  ActiveRecord::SchemaMigration.reset_table_name
end

.schema_stamp_path(type) ⇒ Object



179
180
181
182
183
184
# File 'lib/cardio.rb', line 179

def schema_stamp_path type
  root_dir = (type == :deck_cards ? root : gem_root)
  stamp_dir = ENV['SCHEMA_STAMP_PATH'] || File.join(root_dir, 'db')

  File.join stamp_dir, "version#{schema_suffix type}.txt"
end

.schema_suffix(type) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/cardio.rb', line 149

def schema_suffix type
  case type
  when :core_cards then '_core_cards'
  when :deck_cards then '_deck_cards'
  else ''
  end
end

.set_config(config) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cardio.rb', line 58

def set_config config
  @@config = config
  @@root = config.root

  config.autoload_paths += Dir["#{gem_root}/mod/*/lib/**/"]
  config.autoload_paths += Dir["#{gem_root}/lib/**/"]
  config.autoload_paths += Dir["#{root}/mod/*/lib/**/"]

  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



78
79
80
# File 'lib/cardio.rb', line 78

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

.set_mod_pathsObject



98
99
100
101
102
103
104
# File 'lib/cardio.rb', line 98

def set_mod_paths
  each_mod_path do |mod_path|
    Dir.glob("#{mod_path}/*/initializers").each do |initializers_dir|
      paths['config/initializers'] << initializers_dir
    end
  end
end

.set_paths(paths) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cardio.rb', line 82

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

  add_path 'mod'

  add_path 'db'
  add_path 'db/migrate'
  add_path 'db/migrate_core_cards'
  add_path 'db/migrate_deck_cards', root: root, with: 'db/migrate_cards'
  add_path 'db/seeds', with: 'db/seeds.rb'

  add_path 'config/initializers',  glob: '**/*.rb'
end