module Renderable::Statements

Public Instance Methods

add_renderable( table_name, field_name, field_type = :string, options = {}) click to toggle source

Adds a renderable column to the specified table.

This is reasonably intelligent, and will create any required fields. Thus, if you are adding renderable functionality to an existing field, it will only add the ‘rendered’ version of the field.

Parameters

table_name

the name of the table to which to add the field(s)

field_name

the base name of the field that will be added

field_type

the type of the field that will be added (defaults to @:string@)

options

any additional options to be passed to add_column

# File lib/renderable/schema.rb, line 27
def add_renderable( table_name, field_name, field_type = :string, options = {})
  raise ArgumentError "Please specify name of table in add_renderable call in your migration" if table_name.blank?
  raise ArgumentError "Please specify name of field in add_renderable call in your migration" if field_name.blank?

  # do we have a suffix specified
  suffix = options.delete(:suffix) { |k| '_rendered' }

  # bugfix: if we don’t specify a field type but do specify options, it snarfs things up, so resolve that here
  # @TODO: is there a better way of doing this in Ruby?
  if field_type.is_a? Hash

    # copy field_type hash to options
    options = field_type

    # default field_type again
    field_type = :string

  end

  # add base column
  add_column table_name, field_name, field_type, options unless column_exists?(table_name, field_name)

  # rendered column
  add_column table_name, "#{field_name}#{suffix}", field_type, options
end
remove_renderable( table_name, field_name, suffix = '_rendered' ) click to toggle source

Removes a renderable field from the specified table.

Parameters

table_name

the name of the table from which to remove the field

field_name

the name of the field to remove

suffix

the custom suffix used when creating the field, if used at all

# File lib/renderable/schema.rb, line 61
def remove_renderable( table_name, field_name, suffix = '_rendered' )

  remove_column table_name, field_name if column_exists?(table_name, field_name)
  remove_column table_name, "#{field_name}#{suffix}" if column_exists?(table_name, field_name)

end