Class: Templatr::Field

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/templatr/field.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reserved_fieldsObject

Reserved field names that the user is not allowed to use “Type” is reserved to differentiate between Templates



23
24
25
# File 'app/models/templatr/field.rb', line 23

def self.reserved_fields
  [:type] + templatable_class.attribute_names.collect(&:to_sym) + templatable_class.reflect_on_all_associations.collect(&:name)
end

.search_suggestions(value = true) ⇒ Object



15
# File 'app/models/templatr/field.rb', line 15

def self.search_suggestions(value = true); where(:search_suggestions => value); end

.templatable_classObject



17
18
19
# File 'app/models/templatr/field.rb', line 17

def self.templatable_class
  self.to_s.gsub(/Field\Z/, '').constantize
end

.valid_field_typesObject



27
28
29
# File 'app/models/templatr/field.rb', line 27

def self.valid_field_types
  %w(string text float integer boolean integer_with_uncertainty select_one select_multiple)
end

.valid_migration_path?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'app/models/templatr/field.rb', line 38

def self.valid_migration_path?(from, to)
  paths = valid_migration_paths[from]
  paths && paths.include?(to.to_sym)
end

.valid_migration_pathsObject

The valid ways to migrate data between field types



32
33
34
35
36
# File 'app/models/templatr/field.rb', line 32

def self.valid_migration_paths
  { :string          => [:select_one, :select_multiple, :text],
    :select_one      => [:string, :select_multiple, :text]
  }.with_indifferent_access
end

Instance Method Details

#attribute_typeObject

What attribute type should glint use to store this field’s values



101
102
103
# File 'app/models/templatr/field.rb', line 101

def attribute_type
  (float? || integer? || text? || boolean? ? field_type : 'string').to_sym
end

#boolean?Boolean

Returns:

  • (Boolean)


60
# File 'app/models/templatr/field.rb', line 60

def boolean?;                  field_type == 'boolean' end

#can_change_type?Boolean

Don’t allow changes to the type if the field is saved

Returns:

  • (Boolean)


81
82
83
# File 'app/models/templatr/field.rb', line 81

def can_change_type?
  new_record? || self.class.valid_migration_paths[self.field_type].present?
end

#common?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/templatr/field.rb', line 72

def common?
  template_id.nil?
end

#facet?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/templatr/field.rb', line 76

def facet?
  include_in_search_form? || search_suggestions?
end

#facet_nameObject



105
106
107
# File 'app/models/templatr/field.rb', line 105

def facet_name
  :"field_#{id}"
end

#field_type=(value) ⇒ Object

Coerce the field_type to a string at all times so testing for it is easier



94
95
96
# File 'app/models/templatr/field.rb', line 94

def field_type=(value)
  super(value.to_s)
end

#float?Boolean

Returns:

  • (Boolean)


61
# File 'app/models/templatr/field.rb', line 61

def float?;                    field_type == 'float' end

#integer?Boolean

Returns:

  • (Boolean)


62
# File 'app/models/templatr/field.rb', line 62

def integer?;                  field_type == 'integer' end

#integer_with_uncertainty?Boolean

Returns:

  • (Boolean)


63
# File 'app/models/templatr/field.rb', line 63

def integer_with_uncertainty?; field_type == 'integer_with_uncertainty' end

#paramObject



109
110
111
# File 'app/models/templatr/field.rb', line 109

def param
  (self.disambiguate? ? "#{template.name} #{self.name}" : self.name).downcase # param is always case insensitive
end

#scalar?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/models/templatr/field.rb', line 85

def scalar?
  string? || text? || boolean? || float? || integer? || integer_with_uncertainty?
end

#select?Boolean

Returns:

  • (Boolean)


64
# File 'app/models/templatr/field.rb', line 64

def select?;                   field_type == 'select_one' || field_type == 'select_multiple' end

#select_multiple?Boolean

Returns:

  • (Boolean)


66
# File 'app/models/templatr/field.rb', line 66

def select_multiple?;          field_type == 'select_multiple' end

#select_one?Boolean

Returns:

  • (Boolean)


65
# File 'app/models/templatr/field.rb', line 65

def select_one?;               field_type == 'select_one' end

#string?Boolean

Returns:

  • (Boolean)


58
# File 'app/models/templatr/field.rb', line 58

def string?;                   field_type == 'string' end

#text?Boolean

Returns:

  • (Boolean)


59
# File 'app/models/templatr/field.rb', line 59

def text?;                     field_type == 'text' end

#to_sObject



68
69
70
# File 'app/models/templatr/field.rb', line 68

def to_s
  self.name
end

#valid_migration_pathsObject



43
44
45
# File 'app/models/templatr/field.rb', line 43

def valid_migration_paths
  new_record? ? self.class.valid_field_types : [self.field_type] + Array(self.class.valid_migration_paths[self.field_type])
end

#vector?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/templatr/field.rb', line 89

def vector?
  !scalar?
end