Class: Newsletter::Piece

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/newsletter/piece.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object

respond for its named methods for its element’s template these values will be in its locals hash



64
65
66
67
68
69
70
# File 'app/models/newsletter/piece.rb', line 64

def method_missing(*args)
  if args.length == 1 and locals.has_key?(args[0].to_sym)
    locals[args[0].to_sym]
  else
    super
  end
end

Instance Attribute Details

#field_values_attributesObject

Returns the value of attribute field_values_attributes.



25
26
27
# File 'app/models/newsletter/piece.rb', line 25

def field_values_attributes
  @field_values_attributes
end

Instance Method Details

#fieldsObject

returns a pieces fields



43
44
45
# File 'app/models/newsletter/piece.rb', line 43

def fields
  element.try(:fields).try(:uniq) || []
end

#localsObject

returns locals to be used in its Newsletter::Element design



33
34
35
36
37
38
39
40
# File 'app/models/newsletter/piece.rb', line 33

def locals
  return @locals if @locals.present?
  @locals = Hash.new
  fields.each do |field|
    @locals[field.name.to_sym] = field.value_for_piece(self)
  end
  @locals
end

#respond_to?(my_method, use_private = false) ⇒ Boolean

whether it can respond to a ‘method’ in its locals hash

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'app/models/newsletter/piece.rb', line 53

def respond_to?(my_method,use_private=false)
  return true if super
  if locals.keys.include?(my_method.to_sym)
    true
  else
    false
  end
end

#save(*args) ⇒ Object

:nodoc override save for transaction to set its field values



73
74
75
76
77
78
# File 'app/models/newsletter/piece.rb', line 73

def save(*args)
  transaction do 
    set_field_values
    super
  end
end