Module: Netfira::WebConnect::Models

Defined in:
lib/netfira/web_connect/models.rb

Defined Under Namespace

Classes: Image, Order, OrderLine, Session, Setting, Shop, Table

Class Method Summary collapse

Class Method Details

.materializeObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# File 'lib/netfira/web_connect/models.rb', line 6

def self.materialize

  # Scrap any existing definitions
  Models.constants.each do |name|
    klass = Models.const_get(name)
    Models.__send__ :remove_const, name if klass < Model::Record || klass < Model::Relation
  end

  # Load pre-defined models
  Dir[File.expand_path('../models/*.rb', __FILE__)].each { |p| load p }

  table_names = []
  tables_to_localize = []
  tables_to_relate = []

  Model.connection.tables.reject{|x| x == Netfira::WebConnect.schema_migrations_table_name }.each do |table_name|
    unprefixed_table_name = unprefix_table_name(table_name)
    if unprefixed_table_name.nil? or unprefixed_table_name[0] == '_'
      next
    elsif l10n_table_name? table_name
      tables_to_localize << l10n_table_owner_name(unprefixed_table_name)
    elsif unprefixed_table_name =~ /\A(.+)_to_(.+)\z/
      tables_to_relate << [$1, $2]
    else
      table_names << unprefixed_table_name
    end
  end

  table_props = Models::Table.all.map{ |model| [model.name, model] }.to_h

  table_names.each do |name|
    props = table_props[name]
    next unless props
    Model::Record.materialize name.camelize.singularize,
                              tables_to_localize.include?(name),
                              props
  end

  tables_to_relate.each do |names|
    Model::Relation.materialize *names.map{ |x| x.camelize.singularize }
  end

end