Class: Mack::Genosaurus::Orm::ModelColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/mack-orm/model_column.rb

Overview

Used to represent a ‘column’ from the param cols or columns for generators.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name, column_unsplit) ⇒ ModelColumn

Takes in the model_name (user, post, etc…) and the column (username:string, body:text, etc…)



15
16
17
18
19
20
# File 'lib/mack-orm/model_column.rb', line 15

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

Instance Attribute Details

#column_nameObject

The name of the column.



8
9
10
# File 'lib/mack-orm/model_column.rb', line 8

def column_name
  @column_name
end

#column_typeObject

The type of the column. Ie. string, integer, datetime, etc…



10
11
12
# File 'lib/mack-orm/model_column.rb', line 10

def column_type
  @column_type
end

#model_nameObject

The name of the model associated with the column. Ie. user, post, etc…



12
13
14
# File 'lib/mack-orm/model_column.rb', line 12

def model_name
  @model_name
end

Instance Method Details

#form_fieldObject

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, :label => true %>"
Mack::Generator::ColumnObject.new("Post", "body:text").form_field
  => "<%= :post.text_area :body, :label => true %>"


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mack-orm/model_column.rb', line 29

def form_field
  case self.column_type
  when "text"
    %{<%= :#{self.model_name}.text_area :#{self.column_name}, :label => true %>}
  when "date"
    %{<%= :#{self.model_name}.date_select :#{self.column_name}, :label => true %>}
  when "date_time"
    %{<%= :#{self.model_name}.date_time_select :#{self.column_name}, :label => true %>}
  else
    case self.column_name.downcase
    when /password/
      %{<%= :#{self.model_name}.password_field :#{self.column_name}, :label => true %>}
    else
      %{<%= :#{self.model_name}.text_field :#{self.column_name}, :label => true %>}
    end
  end
end