Module: Renderable::Statements

Defined in:
lib/renderable/schema.rb

Instance Method Summary collapse

Instance Method Details

#add_renderable(table_name, field_name, field_type = :string, options = {}) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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') ⇒ Object

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



61
62
63
64
65
66
# 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