Module: ForestLiana::Collection::ClassMethods

Defined in:
lib/forest_liana/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_record_class=(value) ⇒ Object

Sets the attribute active_record_class

Parameters:

  • value

    the value to set the attribute active_record_class to.



5
6
7
# File 'lib/forest_liana/collection.rb', line 5

def active_record_class=(value)
  @active_record_class = value
end

#collection_nameObject

Returns the value of attribute collection_name.



6
7
8
# File 'lib/forest_liana/collection.rb', line 6

def collection_name
  @collection_name
end

#is_read_onlyObject

Returns the value of attribute is_read_only.



7
8
9
# File 'lib/forest_liana/collection.rb', line 7

def is_read_only
  @is_read_only
end

#is_searchableObject

Returns the value of attribute is_searchable.



8
9
10
# File 'lib/forest_liana/collection.rb', line 8

def is_searchable
  @is_searchable
end

Instance Method Details

#action(name, opts = {}) ⇒ Object



26
27
28
29
# File 'lib/forest_liana/collection.rb', line 26

def action(name, opts = {})
  opts[:name] = name
  model.actions << ForestLiana::Model::Action.new(opts)
end

#belongs_to(name, opts, &block) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/forest_liana/collection.rb', line 128

def belongs_to(name, opts, &block)
  model.fields << opts.merge({
    field: name,
    is_virtual: true,
    is_searchable: false,
    type: 'String'
  })

  define_method(name) { self.data[name] } if smart_collection?

  if serializer_name && ForestLiana::UserSpace.const_defined?(
      serializer_name)
    ForestLiana::UserSpace.const_get(serializer_name).class_eval do
      has_one(name, name: name, include_data: true, &block)
    end
  end
end

#collection(collection_name, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/forest_liana/collection.rb', line 10

def collection(collection_name, opts = {})
  self.collection_name = find_name(collection_name).to_s
  self.is_read_only = opts[:read_only] || false
  self.is_searchable = opts[:is_searchable] || false

  # NOTICE: Creates dynamically the serializer if it's a Smart Collection.
  if smart_collection? &&
      !ForestLiana::UserSpace.const_defined?(serializer_name)

    ForestLiana.names_overriden[self] = self.collection_name.to_s

    ForestLiana::SerializerFactory.new(is_smart_collection: true)
      .serializer_for(self)
  end
end

#field(name, opts, &block) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/forest_liana/collection.rb', line 40

def field(name, opts, &block)
  # TODO: Handle empty name

  if opts.key?(:isRequired)
    FOREST_LOGGER.warn "DEPRECATION WARNING: isRequired on field \"#{name}\" is deprecated. Please use is_required."
    opts[:is_required] = !!opts[:isRequired]
    opts.delete(:isRequired)
  end
  if opts.key?(:isReadOnly)
    FOREST_LOGGER.warn "DEPRECATION WARNING: isReadOnly on field \"#{name}\" is deprecated. Please use is_read_only."
    opts[:is_read_only] = !!opts[:isReadOnly]
    opts.delete(:isReadOnly)
  end
  if opts.key?(:isFilterable)
    FOREST_LOGGER.warn "DEPRECATION WARNING: isFilterable on field \"#{name}\" is deprecated. Please use is_filterable."
    opts[:is_filterable] = !!opts[:isFilterable]
    opts.delete(:isFilterable)
  end
  if opts.key?(:isSortable)
    FOREST_LOGGER.warn "DEPRECATION WARNING: isSortable on field \"#{name}\" is deprecated. Please use is_sortable."
    opts[:is_sortable] = !!opts[:isSortable]
    opts.delete(:isSortable)
  end

  opts[:is_read_only] = true unless opts.has_key?(:is_read_only)
  opts[:is_read_only] = false if opts.has_key?(:set)
  opts[:is_required] = false unless opts.has_key?(:is_required)
  opts[:type] = "String" unless opts.has_key?(:type)
  opts[:default_value] = nil unless opts.has_key?(:default_value)
  opts[:integration] = nil unless opts.has_key?(:integration)
  opts[:reference] = nil unless opts.has_key?(:reference)
  opts[:inverse_of] = nil unless opts.has_key?(:inverse_of)
  opts[:relationships] = nil unless opts.has_key?(:relationships)
  opts[:widget] = nil unless opts.has_key?(:widget)
  opts[:validations] = [] unless opts.has_key?(:validations)

  model.fields << opts.merge({
    field: name,
    is_filterable: !!opts[:is_filterable],
    is_sortable: !!opts[:is_sortable],
    is_virtual: true
  })

  define_method(name) { self.data[name] } if smart_collection?

  if serializer_name && ForestLiana::UserSpace.const_defined?(
      serializer_name)
    ForestLiana::UserSpace.const_get(serializer_name).class_eval do
      if block
        # NOTICE: Smart Field case.
        compute_value = lambda do |object|
          begin
            object.instance_eval(&block)
          rescue => exception
            FOREST_REPORTER.report exception
            FOREST_LOGGER.error "Cannot retrieve the " + name.to_s + " value because of an " \
              "internal error in the getter implementation: " + exception.message
            nil
          end
        end

        attribute(name, &compute_value)
      else
        # NOTICE: Smart Collection field case.
        attribute(name)
      end
    end
  end
end

#has_many(name, opts, &block) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/forest_liana/collection.rb', line 110

def has_many(name, opts, &block)
  model.fields << opts.merge({
    field: name,
    is_virtual: true,
    is_searchable: false,
    type: ['String']
  })

  define_method(name) { self.data[name] } if smart_collection?

  if serializer_name && ForestLiana::UserSpace.const_defined?(
      serializer_name)
    ForestLiana::UserSpace.const_get(serializer_name).class_eval do
      has_many(name, name: name)
    end
  end
end

#search_fields(fields) ⇒ Object



36
37
38
# File 'lib/forest_liana/collection.rb', line 36

def search_fields(fields)
  model.search_fields = fields
end

#segment(name, opts = {}, &block) ⇒ Object



31
32
33
34
# File 'lib/forest_liana/collection.rb', line 31

def segment(name, opts = {}, &block)
  opts[:name] = name
  model.segments << ForestLiana::Model::Segment.new(opts, &block)
end