Class: Metka::Model

Inherits:
Module
  • Object
show all
Defined in:
lib/metka/model.rb

Instance Method Summary collapse

Constructor Details

#initialize(column:, **options) ⇒ Model

Returns a new instance of Model.



9
10
11
12
# File 'lib/metka/model.rb', line 9

def initialize(column: , **options)
  @column = column
  @options = options
end

Instance Method Details

#included(base) ⇒ Object



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
# File 'lib/metka/model.rb', line 14

def included(base)
  column = @column
  parser = ->(tags) {
    @options[:parser] ? @options[:parser].call(tags) : Metka.config.parser.instance.call(tags)
  }

  search_by_tags = ->(model, tags, column, **options) {
    parsed_tag_list = parser.call(tags)
    if options[:without].present?
      model.where.not(::Metka::QueryBuilder.new.call(model, column, parsed_tag_list, options))
    else
      return model.none if parsed_tag_list.empty?
      model.where(::Metka::QueryBuilder.new.call(model, column, parsed_tag_list, options))
    end
  }

  base.class_eval do
    scope "with_all_#{column}", ->(tags) { search_by_tags.call(self, tags, column) }
    scope "with_any_#{column}", ->(tags) { search_by_tags.call(self, tags, column, { any: true }) }
    scope "without_all_#{column}", ->(tags) { search_by_tags.call(self, tags, column, { exclude_all: true, without: true }) }
    scope "without_any_#{column}", ->(tags) { search_by_tags.call(self, tags, column, { exclude_any: true, without: true }) }
  end

  base.define_method(column.singularize + '_list=') do |v|
    self.write_attribute(column, parser.call(v).to_a)
    self.write_attribute(column, nil) if self.send(column).empty?
  end

  base.define_method(column.singularize + '_list') do
    parser.call(self.send(column))
  end
end