Class: Schofield::Generators::Attribute

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

Constant Summary collapse

ATTACHMENT_IMAGE_NAMES =
%w( image thumbnail enlargement photo avatar )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column) ⇒ Attribute

Returns a new instance of Attribute.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/schofield/attribute.rb', line 12

def initialize column

  if (match_data = column.name.match(/(.+)_file_name$/))
    @attachment_name = match_data[1]
    @attachment      = true
    @image           = !!(@attachment_name =~ /#{ATTACHMENT_IMAGE_NAMES.join('|')}/)
  end

  if (match_data = column.name.match(/^(.+)_id$/))
    model_name = match_data[1]
    if Levels.exists?(model_name)
      @model_name = model_name
      @reference  = true
    end
  end

  @name        = column.name
  @required    = !column.null
  @type        = column.type
  @max_length  = column.type == :string ? column.limit : nil
  @max_number  = column.type == :integer && !@reference ? 256**column.limit / 2 - 1 : nil

end

Instance Attribute Details

#attachment_nameObject (readonly)

Returns the value of attribute attachment_name.



9
10
11
# File 'lib/generators/schofield/attribute.rb', line 9

def attachment_name
  @attachment_name
end

#max_lengthObject (readonly)

Returns the value of attribute max_length.



9
10
11
# File 'lib/generators/schofield/attribute.rb', line 9

def max_length
  @max_length
end

#max_numberObject (readonly)

Returns the value of attribute max_number.



9
10
11
# File 'lib/generators/schofield/attribute.rb', line 9

def max_number
  @max_number
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



9
10
11
# File 'lib/generators/schofield/attribute.rb', line 9

def model_name
  @model_name
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/generators/schofield/attribute.rb', line 9

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/generators/schofield/attribute.rb', line 9

def type
  @type
end

Instance Method Details

#accessible_nameObject



76
77
78
# File 'lib/generators/schofield/attribute.rb', line 76

def accessible_name
  actual_name unless fingerprint? || @name == 'slug'
end

#actual_nameObject



68
69
70
# File 'lib/generators/schofield/attribute.rb', line 68

def actual_name
  @attachment_name || @model_name || @name
end

#attachment?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/generators/schofield/attribute.rb', line 40

def attachment?
  @attachment == true
end

#boolean?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/generators/schofield/attribute.rb', line 64

def boolean?
  type == :boolean
end

#fingerprint?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/generators/schofield/attribute.rb', line 72

def fingerprint?
  !!(@name =~ /_fingerprint$/)
end

#image?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/generators/schofield/attribute.rb', line 44

def image?
  @image
end

#reference?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/generators/schofield/attribute.rb', line 36

def reference?
  @reference == true
end

#required?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/generators/schofield/attribute.rb', line 48

def required?
  @required && !@attachment
end

#required_attachment?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/generators/schofield/attribute.rb', line 56

def required_attachment?
  @attachment && @required
end

#text?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/generators/schofield/attribute.rb', line 60

def text?
  type == :text
end

#to_columnObject



80
81
82
83
84
85
86
87
88
# File 'lib/generators/schofield/attribute.rb', line 80

def to_column
  "'#{actual_name.humanize}', lambda { |x| " +
  case
  when image?       then "image_tag(x.#{@attachment_name}.url(:admin), :alt => '', :class => 'thumbnail')"
  when attachment?  then "link_to('#{actual_name.humanize}', x.#{@attachment_name}.url)"
  when boolean?     then "x.#{actual_name} ? '✓' : ''"
  else                   "x.#{actual_name}"
  end + ' }'
end

#validate_presence?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/generators/schofield/attribute.rb', line 52

def validate_presence?
  required? && @name != 'position' && !boolean? && !fingerprint? && @name != 'slug'
end

#weightObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/schofield/attribute.rb', line 90

def weight
  case
  when image?               then 1
  when @name == 'name'      then 2
  when @name == 'title'     then 3
  when reference?           then 4
  when @name == 'published' then 6
  else                           5
  end
end