public
Description: All the extra stuff you could want for the Mack Framework.
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack-more.git
mack-more / mack-orm / lib / mack-orm / model_column.rb
100644 41 lines (37 sloc) 1.589 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Mack
  module Genosaurus # :nodoc:
    module Orm # :nodoc:
      # Used to represent a 'column' from the param cols or columns for generators.
      class ModelColumn # :nodoc:
      
        # The name of the column.
        attr_accessor :column_name
        # The type of the column. Ie. string, integer, datetime, etc...
        attr_accessor :column_type
        # The name of the model associated with the column. Ie. user, post, etc...
        attr_accessor :model_name
      
        # Takes in the model_name (user, post, etc...) and the column (username:string, body:text, etc...)
        def initialize(model_name, column_unsplit)
          self.model_name = model_name.singular.underscore
          cols = column_unsplit.split(":")
          self.column_name = cols.first#.underscore
          self.column_type = cols.last#.underscore
        end
 
        # Generates the appropriate HTML form field for the type of column represented.
        #
        # Examples:
        # Mack::Generator::ColumnObject.new("user", "username:string").form_field
        # => "<%= :user.text_field :username %>"
        # Mack::Generator::ColumnObject.new("Post", "body:text").form_field
        # => "<%= :post.text_area :body %>"
        def form_field
          case self.column_type
          when "text"
            %{<%= :#{self.model_name}.text_area :#{self.column_name} %>}
          else
            %{<%= :#{self.model_name}.text_field :#{self.column_name} %>}
          end
        end
      
      end # ModelColumn
    end # Orm
  end # Generator
end # Mack