Class: Step8DomainColumns

Inherits:
KDomain::DomainModel::Step show all
Defined in:
lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb

Overview

columns to models

Instance Attribute Summary collapse

Attributes inherited from KDomain::DomainModel::Step

#domain_data, #opts, #valid

Instance Method Summary collapse

Methods inherited from KDomain::DomainModel::Step

#camel, #database, #database=, #database_foreign_keys, #database_tables, #domain, #domain_models, #find_foreign_table, #find_rails_structure_models, #find_table_for_model, #guard, #initialize, #investigate, #issues, #rails_resource, #rails_resource_controllers, #rails_resource_models, #rails_resource_models=, #rails_resource_routes, #rails_resource_routes=, #rails_structure, #rails_structure_controllers, #rails_structure_controllers=, #rails_structure_models, #rails_structure_models=, run, #table_name_exist?, #warning, #write

Constructor Details

This class inherits a constructor from KDomain::DomainModel::Step

Instance Attribute Details

#column_nameObject (readonly)

Returns the value of attribute column_name.



8
9
10
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 8

def column_name
  @column_name
end

#column_symbolObject (readonly)

Returns the value of attribute column_symbol.



9
10
11
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 9

def column_symbol
  @column_symbol
end

#domain_columnObject (readonly)

Returns the value of attribute domain_column.



6
7
8
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 6

def domain_column
  @domain_column
end

#domain_modelObject (readonly)

Returns the value of attribute domain_model.



5
6
7
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 5

def domain_model
  @domain_model
end

#rails_modelObject (readonly)

Returns the value of attribute rails_model.



7
8
9
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 7

def rails_model
  @rails_model
end

Instance Method Details

#callObject



11
12
13
14
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 11

def call
  @debug = true
  enrich_columns
end

#enrich_columnsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 16

def enrich_columns
  # .select {|m| m[:name] == 'app_user'}
  domain_models.each do |model|
    @domain_model = enrich_model(model)
    # this will be nil if there is no rails model code

    # log.warn domain_model[:name]
    domain_model[:columns].each do |column|
      @domain_column = column
      @column_name = column[:name]
      @column_symbol = column[:name].to_sym

      column[:structure_type] = structure_type
    end
  end
end

#enrich_model(model) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 33

def enrich_model(model)
  # NOTE: THIS MAY GET MOVED TO DomainModel::Load#enrichment
  @rails_model = find_rails_structure_models(model[:name])

  model[:file] = @rails_model[:file]

  log.error "Rails model not found for: #{model[:name]}" unless @rails_model

  model
end

#foreign_relationship?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 59

def foreign_relationship?
  return false if rails_model.nil? || rails_model[:behaviours].nil? || rails_model[:behaviours][:belongs_to].nil?

  column_name = domain_column[:name].to_sym
  rails_model[:behaviours][:belongs_to].any? { |belong| belong[:opts][:foreign_key].to_sym == column_name }
end

#structure_typeObject

Need some configurable data dictionary where by _token can be setup on a project by project basis



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/k_domain/domain_model/transform_steps/step8_domain_columns.rb', line 46

def structure_type
  return :primary_key         if domain_model[:pk][:name] == column_name
  return :foreign_key         if foreign_relationship?
  return :foreign_type        if column_symbol == :context_type || column_symbol == :element_type

  return :timestamp           if column_symbol == :created_at || column_symbol == :updated_at
  return :deleted_at          if column_symbol == :deleted_at
  return :encrypted_password  if column_symbol == :encrypted_password
  return :token               if column_name.ends_with?('_token') || column_name.ends_with?('_token_iv')

  :data
end