Class: Mack::Genosaurus::ModelColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/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/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.singular.underscore
  self.column_type = cols.last.singular.underscore
end

Instance Attribute Details

#column_nameObject

The name of the column.



8
9
10
# File 'lib/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/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/model_column.rb', line 12

def model_name
  @model_name
end

Instance Method Details

#form_element_idObject

Examples:

Mack::Generator::ColumnObject.new("user", "username:string").form_element_id # => "user_username"
Mack::Generator::ColumnObject.new("Post", "body:text").form_element_id # => "post_body"


32
33
34
# File 'lib/model_column.rb', line 32

def form_element_id
  "#{self.model_name}_#{self.column_name}"
end

#form_element_nameObject

Examples:

Mack::Generator::ColumnObject.new("user", "username:string").form_element_name # => "user[username]"
Mack::Generator::ColumnObject.new("Post", "body:text").form_element_name # => "post[body]"


25
26
27
# File 'lib/model_column.rb', line 25

def form_element_name
  "#{self.model_name}[#{self.column_name}]"
end

#form_fieldObject

Generates the appropriate HTML form field for the type of column represented.

Examples:

Mack::Generator::ColumnObject.new("user", "username:string").form_field 
  => "<input type="text" name="user[username]" id="user_username" size="30" value="<%= user.username %>" />"
Mack::Generator::ColumnObject.new("Post", "body:text").form_field
  => "<textarea name="post[body]" id="post_id"><%= post.body %></textarea>"


43
44
45
46
47
48
49
50
# File 'lib/model_column.rb', line 43

def form_field
  case self.column_type
  when "text"
    %{<textarea name="#{self.form_element_name}" id="#{self.form_element_id}" cols="60" rows="20"><%= @#{self.model_name}.#{self.column_name} %></textarea>}
  else
    %{<input type="text" name="#{self.form_element_name}" id="#{self.form_element_id}" size="30" value="<%= @#{self.model_name}.#{self.column_name} %>" />}
  end
end