class Schofield::Generators::Attribute

Constants

ATTACHMENT_IMAGE_NAMES

Attributes

attachment_name[R]
max_length[R]
max_number[R]
model_name[R]
name[R]
type[R]

Public Class Methods

new(column) click to toggle source
# 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

Public Instance Methods

accessible_name() click to toggle source
# File lib/generators/schofield/attribute.rb, line 76
def accessible_name
  actual_name unless fingerprint? || @name == 'slug'
end
actual_name() click to toggle source
# File lib/generators/schofield/attribute.rb, line 68
def actual_name
  @attachment_name || @model_name || @name
end
attachment?() click to toggle source
# File lib/generators/schofield/attribute.rb, line 40
def attachment?
  @attachment == true
end
boolean?() click to toggle source
# File lib/generators/schofield/attribute.rb, line 64
def boolean?
  type == :boolean
end
fingerprint?() click to toggle source
# File lib/generators/schofield/attribute.rb, line 72
def fingerprint?
  !!(@name =~ /_fingerprint$/)
end
image?() click to toggle source
# File lib/generators/schofield/attribute.rb, line 44
def image?
  @image
end
reference?() click to toggle source
# File lib/generators/schofield/attribute.rb, line 36
def reference?
  @reference == true
end
required?() click to toggle source
# File lib/generators/schofield/attribute.rb, line 48
def required?
  @required && !@attachment
end
required_attachment?() click to toggle source
# File lib/generators/schofield/attribute.rb, line 56
def required_attachment?
  @attachment && @required
end
text?() click to toggle source
# File lib/generators/schofield/attribute.rb, line 60
def text?
  type == :text
end
to_column() click to toggle source
# 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?() click to toggle source
# File lib/generators/schofield/attribute.rb, line 52
def validate_presence?
  required? && @name != 'position' && !boolean? && !fingerprint? && @name != 'slug'
end
weight() click to toggle source
# 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