Class: ForestLiana::SchemaAdapter

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/schema_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ SchemaAdapter

Returns a new instance of SchemaAdapter.



3
4
5
# File 'app/services/forest_liana/schema_adapter.rb', line 3

def initialize(model)
  @model = model
end

Instance Method Details

#performObject



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/forest_liana/schema_adapter.rb', line 7

def perform
  add_columns
  add_associations

  collection.fields.sort_by!.with_index { |k, idx| [k[:field].to_s, idx] }

  # NOTICE: Add ActsAsTaggable fields
  if @model.try(:taggable?) && @model.respond_to?(:acts_as_taggable) &&
    @model.acts_as_taggable.respond_to?(:to_a)
    @model.acts_as_taggable.to_a.each do |key, value|
      field = collection.fields.find { |x| x[:field] == key.to_s }

      if field
        field[:type] = 'String'
        field[:reference] = nil
        field[:inverse_of] = nil

        collection.fields.delete_if do |f|
          ['taggings', 'base_tags', 'tag_taggings'].include?(f[:field])
        end
      end
    end
  end

  # NOTICE: Add Devise fields
  if @model.respond_to?(:devise_modules?)
    collection.actions << ForestLiana::Model::Action.new({
      id: "#{collection.name}.Change password",
      name: "Change password",
      fields: [{
        field: 'New password',
        type: 'String'
      }]
    })

    collection.fields.each do |field|
      if field[:field] == 'encrypted_password'
        field[:field] = 'password'
      end
    end
  end

  # NOTICE: Define an automatic segment for each STI child model.
  if is_sti_parent?
    if @model.descendants.empty?
      FOREST_LOGGER.warn "Looks like your Rails STI parent model named \"#{@model.name}\" " +
        "does not have any child model. If you want to deactivate the STI feature, add " +
        "\"self.inheritance_column = nil\" in the model."
    end

    column_type = @model.inheritance_column
    @model.descendants.each do |submodel_sti|
      type = submodel_sti.sti_name
      name = type.pluralize
      collection.segments << ForestLiana::Model::Segment.new({
        id: name,
        name: name,
        where: lambda { { column_type => type } }
      })
    end
  end

  collection
end