module Schofield module Generators class Attribute ATTACHMENT_IMAGE_NAMES = %w( image thumbnail enlargement photo avatar ) attr_reader :model_name, :name, :max_number, :max_length, :attachment_name, :type 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$/)) @reference = true @model_name = match_data[1] 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 def reference? @reference == true end def attachment? @attachment == true end def image? @image == 0 end def required? @required && !@attachment end def required_attachment? @attachment && @required end def text? type == :text end def boolean? type == :boolean end def actual_name @attachment_name || @model_name || @name end def to_column case when image? then "'#{actual_name.humanize} ', lambda { |x| image_tag(x.#{@attachment_name}.url(:thumbnail), :alt => '', :class => 'thumbnail') }" when attachment? then "link_to('#{actual_name.humanize}', x.#{@attachment_name}.url)" else "'#{actual_name.humanize}', lambda { |x| x.#{actual_name} }" end end end end end