Class: ScopedSearch::Definition::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/scoped_search/definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, options = {}) ⇒ Field

Returns a new instance of Field.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/scoped_search/definition.rb', line 54

def initialize(definition, options = {})
  @definition = definition
  case options
  when Symbol, String
    @field = field.to_sym
  when Hash
    @field = options.delete(:on)

    # Set attributes from options hash
    @relation         = options[:in]
    @only_explicit    = !!options[:only_explicit]
    @default_operator = options[:default_operator] if options.has_key?(:default_operator)
  end
  
  # Store this field is the field array
  definition.fields[@field] ||= self
  definition.unique_fields   << self
  
  # Store definition for alias / aliases as well
  definition.fields[options[:alias]] ||= self                    if options[:alias]
  options[:aliases].each { |al| definition.fields[al] ||= self } if options[:aliases]        
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



7
8
9
# File 'lib/scoped_search/definition.rb', line 7

def definition
  @definition
end

#fieldObject (readonly)

Returns the value of attribute field.



7
8
9
# File 'lib/scoped_search/definition.rb', line 7

def field
  @field
end

#only_explicitObject (readonly)

Returns the value of attribute only_explicit.



7
8
9
# File 'lib/scoped_search/definition.rb', line 7

def only_explicit
  @only_explicit
end

#relationObject (readonly)

Returns the value of attribute relation.



7
8
9
# File 'lib/scoped_search/definition.rb', line 7

def relation
  @relation
end

Instance Method Details

#columnObject

Find the relevant column definition in the AR class



18
19
20
# File 'lib/scoped_search/definition.rb', line 18

def column
  klass.columns_hash[field.to_s]
end

#date?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/scoped_search/definition.rb', line 30

def date?
  type == :date
end

#datetime?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/scoped_search/definition.rb', line 26

def datetime?
  [:datetime, :time, :timestamp].include?(type)
end

#default_operatorObject

Returns the default search operator for this field.



47
48
49
50
51
52
# File 'lib/scoped_search/definition.rb', line 47

def default_operator
  @default_operator ||= case type
    when :string, :text then :like
    else :eq
  end
end

#klassObject



9
10
11
12
13
14
15
# File 'lib/scoped_search/definition.rb', line 9

def klass
  if relation
    definition.klass.reflections[relation].klass
  else
    definition.klass
  end
end

#numerical?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/scoped_search/definition.rb', line 38

def numerical?
  [:integer, :double, :float, :decimal].include?(type)
end

#temporal?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/scoped_search/definition.rb', line 34

def temporal?
  datetime? || date?
end

#textual?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/scoped_search/definition.rb', line 42

def textual?
  [:string, :text].include?(type)
end

#typeObject



22
23
24
# File 'lib/scoped_search/definition.rb', line 22

def type
  column.type
end