Class: CMS::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/cms/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, format, options) ⇒ Attribute

Returns a new instance of Attribute.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cms/attribute.rb', line 4

def initialize name, format, options
  @name = name
  @format = format
  @options = options
  @attr_options = if self.format.boolean?
    {
      null: false,
      default: false
    }
  else
    {}
  end
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



2
3
4
# File 'lib/cms/attribute.rb', line 2

def format
  @format
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/cms/attribute.rb', line 2

def name
  @name
end

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/cms/attribute.rb', line 2

def options
  @options
end

Instance Method Details

#field_nameObject



66
67
68
69
70
71
72
# File 'lib/cms/attribute.rb', line 66

def field_name
  if reference?
    name + '_id'
  else
    name
  end
end

#file?Boolean

Returns:

  • (Boolean)


24
# File 'lib/cms/attribute.rb', line 24

def file?      ; format.file? end

#form_typeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cms/attribute.rb', line 52

def form_type
  if format.orderable? || format.reference?
    'select'
  elsif format.text? or format.html?
    'text_area'
  elsif format.file?
    'file_field'
  elsif format.boolean?
    'check_box'
  else
    'text_field'
  end
end

#has_index?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/cms/attribute.rb', line 26

def has_index?
  false
end

#has_uniq_index?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/cms/attribute.rb', line 30

def has_uniq_index?
  false
end

#index_nameObject



34
35
36
# File 'lib/cms/attribute.rb', line 34

def index_name
  format.reference? ? "#{name}_id" : name
end

#inject_index_optionsObject



78
79
80
# File 'lib/cms/attribute.rb', line 78

def inject_index_options
  has_uniq_index? ? ", :unique => true" : ''
end

#inject_optionsObject



74
75
76
# File 'lib/cms/attribute.rb', line 74

def inject_options
  CMS::Template.inject_options(@attr_options)
end

#migration_typeObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cms/attribute.rb', line 38

def migration_type
  if format.reference?
    'belongs_to'
  elsif format.orderable?
    'integer'
  elsif format.file?
    'string'
  elsif format.html?
    'text'
  else
    format
  end
end

#orderable?Boolean

Returns:

  • (Boolean)


23
# File 'lib/cms/attribute.rb', line 23

def orderable? ; format.orderable? end

#reference?Boolean

Returns:

  • (Boolean)


22
# File 'lib/cms/attribute.rb', line 22

def reference? ; format.reference? end

#reference_toObject



82
83
84
85
86
# File 'lib/cms/attribute.rb', line 82

def reference_to
  if reference?
    @reference_to ||= CMS::Configuration.types.find{ |t| t.name == @options['reference_to'] }
  end
end

#to_sObject



88
89
90
# File 'lib/cms/attribute.rb', line 88

def to_s
  name
end