Class: Iron::Schema

Inherits:
Object
  • Object
show all
Extended by:
Lexorank::Utils
Defined in:
app/models/iron/schema.rb

Defined Under Namespace

Modules: AutoDumpable Classes: Diff, FileMissing, InvalidSchema, Validation

Constant Summary collapse

DEFAULT_PATH =
"db/cms/schema.json"

Constants included from Lexorank::Utils

Lexorank::Utils::BASE, Lexorank::Utils::MAX_CHAR, Lexorank::Utils::MIN_CHAR

Class Method Summary collapse

Methods included from Lexorank::Utils

get_char, mid, optimal_rank_numeric_interval_for, to_rank, value_between

Class Method Details

.apply(path = nil, prune: false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/iron/schema.rb', line 61

def apply(path = nil, prune: false)
  schema = parse(path)
  validate!(schema, prune:)

  Diff.new(schema).tap do |diff|
    suppress_auto_dump do
      ActiveRecord::Base.transaction do
        import(schema)
        prune!(diff) if prune
      end
    end
  end
end

.auto_dumpObject



98
99
100
101
102
103
104
105
106
107
# File 'app/models/iron/schema.rb', line 98

def auto_dump
  return unless change_noted?
  forget_change

  return unless editable?
  return if auto_dump_suppressed?
  return unless path_for.exist?

  dump
end

.diff(path = nil) ⇒ Object



54
55
56
57
58
59
# File 'app/models/iron/schema.rb', line 54

def diff(path = nil)
  schema = parse(path)
  validate!(schema)

  Diff.new(schema)
end

.dump(path = nil) ⇒ Object



33
34
35
36
37
38
# File 'app/models/iron/schema.rb', line 33

def dump(path = nil)
  path_for(path).tap do |file|
    FileUtils.mkdir_p(file.dirname)
    file.write("#{to_json}\n")
  end
end

.editable?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/models/iron/schema.rb', line 21

def editable?
  editable_in_environment.call(Rails.env)
end

.export_hashObject



44
45
46
47
48
49
50
51
52
# File 'app/models/iron/schema.rb', line 44

def export_hash
  {
    version: "1.0",
    default_locale: Current.&.default_locale&.code,
    locales: export_locales,
    block_definitions: export_block_definitions,
    content_types: export_content_types
  }
end

.forget_changeObject



94
95
96
# File 'app/models/iron/schema.rb', line 94

def forget_change
  ActiveSupport::IsolatedExecutionState[:iron_schema_change_noted] = nil
end

.import(schema) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/iron/schema.rb', line 75

def import(schema)
  suppress_auto_dump do
    ActiveRecord::Base.transaction do
      import_locales(schema[:locales] || [])
      ensure_account!(schema)
      import_block_definitions(schema[:block_definitions] || [])
      populate_block_field_definitions(schema[:block_definitions] || [])
      import_content_types(schema[:content_types] || [])
      resolve_supported_definitions(schema)
      resolve_content_type_references(schema[:content_types] || [])
      restore_default_locale(schema[:default_locale])
    end
  end
end

.locked?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/iron/schema.rb', line 25

def locked?
  !editable?
end

.note_changeObject



90
91
92
# File 'app/models/iron/schema.rb', line 90

def note_change
  ActiveSupport::IsolatedExecutionState[:iron_schema_change_noted] = true
end

.path_for(path = nil) ⇒ Object



29
30
31
# File 'app/models/iron/schema.rb', line 29

def path_for(path = nil)
  Pathname.new(path || Rails.root.join(DEFAULT_PATH))
end

.suppress_auto_dumpObject



109
110
111
112
113
114
115
# File 'app/models/iron/schema.rb', line 109

def suppress_auto_dump
  previous = ActiveSupport::IsolatedExecutionState[:iron_schema_auto_dump_suppressed]
  ActiveSupport::IsolatedExecutionState[:iron_schema_auto_dump_suppressed] = true
  yield
ensure
  ActiveSupport::IsolatedExecutionState[:iron_schema_auto_dump_suppressed] = previous
end

.to_jsonObject



40
41
42
# File 'app/models/iron/schema.rb', line 40

def to_json
  JSON.pretty_generate(export_hash)
end