Class: Schofield::Generators::Attribute
- Inherits:
-
Object
- Object
- Schofield::Generators::Attribute
- Defined in:
- lib/generators/schofield/attribute.rb
Constant Summary collapse
- ATTACHMENT_IMAGE_NAMES =
%w( image thumbnail enlargement photo avatar )
Instance Attribute Summary collapse
-
#attachment_name ⇒ Object
readonly
Returns the value of attribute attachment_name.
-
#max_length ⇒ Object
readonly
Returns the value of attribute max_length.
-
#max_number ⇒ Object
readonly
Returns the value of attribute max_number.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #accessible_name ⇒ Object
- #actual_name ⇒ Object
- #attachment? ⇒ Boolean
- #boolean? ⇒ Boolean
- #fingerprint? ⇒ Boolean
- #image? ⇒ Boolean
-
#initialize(column) ⇒ Attribute
constructor
A new instance of Attribute.
- #reference? ⇒ Boolean
- #required? ⇒ Boolean
- #required_attachment? ⇒ Boolean
- #text? ⇒ Boolean
- #to_column ⇒ Object
- #validate_presence? ⇒ Boolean
- #weight ⇒ Object
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_name ⇒ Object (readonly)
Returns the value of attribute attachment_name.
9 10 11 |
# File 'lib/generators/schofield/attribute.rb', line 9 def @attachment_name end |
#max_length ⇒ Object (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_number ⇒ Object (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_name ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/generators/schofield/attribute.rb', line 9 def name @name end |
#type ⇒ Object (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_name ⇒ Object
76 77 78 |
# File 'lib/generators/schofield/attribute.rb', line 76 def accessible_name actual_name unless fingerprint? || @name == 'slug' end |
#actual_name ⇒ Object
68 69 70 |
# File 'lib/generators/schofield/attribute.rb', line 68 def actual_name @attachment_name || @model_name || @name end |
#attachment? ⇒ Boolean
40 41 42 |
# File 'lib/generators/schofield/attribute.rb', line 40 def @attachment == true end |
#boolean? ⇒ Boolean
64 65 66 |
# File 'lib/generators/schofield/attribute.rb', line 64 def boolean? type == :boolean end |
#fingerprint? ⇒ Boolean
72 73 74 |
# File 'lib/generators/schofield/attribute.rb', line 72 def fingerprint? !!(@name =~ /_fingerprint$/) end |
#image? ⇒ Boolean
44 45 46 |
# File 'lib/generators/schofield/attribute.rb', line 44 def image? @image end |
#reference? ⇒ Boolean
36 37 38 |
# File 'lib/generators/schofield/attribute.rb', line 36 def reference? @reference == true end |
#required? ⇒ Boolean
48 49 50 |
# File 'lib/generators/schofield/attribute.rb', line 48 def required? @required && !@attachment end |
#required_attachment? ⇒ Boolean
56 57 58 |
# File 'lib/generators/schofield/attribute.rb', line 56 def @attachment && @required end |
#text? ⇒ Boolean
60 61 62 |
# File 'lib/generators/schofield/attribute.rb', line 60 def text? type == :text end |
#to_column ⇒ Object
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 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
52 53 54 |
# File 'lib/generators/schofield/attribute.rb', line 52 def validate_presence? required? && @name != 'position' && !boolean? && !fingerprint? && @name != 'slug' end |
#weight ⇒ Object
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 |