Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/forest_liana/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ def collection(collection_name, opts = {})
end

def action(name, opts = {})
return if model.actions.find { |action| action.name == name }

opts[:name] = name
model.actions << ForestLiana::Model::Action.new(opts)
end

def segment(name, opts = {}, &block)
return if model.segments.find { |segment| segment.name == name }

opts[:name] = name
model.segments << ForestLiana::Model::Segment.new(opts, &block)
end
Expand Down Expand Up @@ -74,12 +78,14 @@ def field(name, opts, &block)
opts[:validations] = [] unless opts.has_key?(:validations)
opts[:is_virtual] = true unless opts.has_key?(:polymorphic_key) && opts[:polymorphic_key]

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

add_field(field)

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

if serializer_name && ForestLiana::UserSpace.const_defined?(
Expand Down Expand Up @@ -108,13 +114,15 @@ def field(name, opts, &block)
end

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

add_field(field)

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

if serializer_name && ForestLiana::UserSpace.const_defined?(
Expand All @@ -126,13 +134,15 @@ def has_many(name, opts, &block)
end

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

add_field(field)

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

if serializer_name && ForestLiana::UserSpace.const_defined?(
Expand All @@ -145,6 +155,12 @@ def belongs_to(name, opts, &block)

private

def add_field(field)
return if model.fields.find { |field| field[:field] == name }

model.fields << field
end

def find_name(collection_name)
# TODO: Remove once lianas prior to 2.0.0 are not supported anymore.
model = ForestLiana.models.find { |collection| collection.try(:table_name) == collection_name.to_s }
Expand Down
Loading