lib/generators/schofield/attribute.rb in schofield-0.2.2 vs lib/generators/schofield/attribute.rb in schofield-0.3.0
- old
+ new
@@ -1,87 +1,104 @@
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('|')}/
+ @image = !!(@attachment_name =~ /#{ATTACHMENT_IMAGE_NAMES.join('|')}/)
end
-
+
if (match_data = column.name.match(/^(.+)_id$/))
- @reference = true
- @model_name = match_data[1]
+ 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
-
+
def reference?
@reference == true
end
-
+
def attachment?
@attachment == true
end
-
+
def image?
- @image == 0
+ @image
end
-
+
def required?
@required && !@attachment
end
-
+
+ def validate_presence?
+ required? && @name != 'position' && !boolean? && !fingerprint? && @name != 'slug'
+ 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 fingerprint?
+ !!(@name =~ /_fingerprint$/)
+ end
+
+ def accessible_name
+ actual_name unless fingerprint? || @name == 'slug'
+ end
+
def to_column
"'#{actual_name.humanize}', lambda { |x| " +
case
- when image? then "image_tag(x.#{@attachment_name}.url(:thumbnail), :alt => '', :class => 'thumbnail')"
+ 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
-
+
def weight
case
- when image? then 1
- when @name == 'name' then 2
- when @name == 'title' then 3
- when reference? then 4
- else 5
+ 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
-
+
end
-
+
end
end
\ No newline at end of file