Sha256: 837b6a42b9e6362b6cbfdc4bcefa827b48eea898f3e3bf434b7a98a904818e98

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

module Disclaimer
  class Document < ActiveRecord::Base
    attr_accessible :footer, :header, :title, :name, :segments, :segment_ids
    
    before_save :underscore_name
    
    has_many :segment_holders, :uniq => true
    has_many :segments, :through => :segment_holders, :uniq => true
    accepts_nested_attributes_for :segments
    
    validates :name, :presence => true
    
    def segment_holder_for(segment)
      segment_holders.where(:segment_id => segment.id).first
    end
    
    def to_param
      name
    end
    
    def title
      ((!super || super.empty?) && name.present?) ? name.humanize : super
    end
    
    private
    def method_missing(method, *args)  
      segment = args.first     
      if act_as_list_methods.include?(method.to_sym) and segment.kind_of? Segment
        modify_via_segment_holder_acts_as_list_method(method, segment)
      else
        super(method, *args)
      end     
    end
    
    def act_as_list_methods
      @act_as_list_methods ||= ActiveRecord::Acts::List::InstanceMethods.instance_methods
    end
    
    def modify_via_segment_holder_acts_as_list_method(method, segment)
      segment_holder = segment_holder_for(segment)
      raise segment_not_associated_message(method, segment) unless segment_holder     
      segment_holder.send(method)  
    end
    
    def segment_not_associated_message(method, segment)
      <<EOF
The segment is not associated with the document, and therefore document.#{method}(segment) is not valid: 
#{inspect} 
#{segment.inspect}
EOF
    end
    
    def underscore_name
      self.name = self.name.downcase.gsub(/\s+/, '_')
    end
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
disclaimer-0.1.0 app/models/disclaimer/document.rb
disclaimer-0.0.7 app/models/disclaimer/document.rb
disclaimer-0.0.6 app/models/disclaimer/document.rb
disclaimer-0.0.5 app/models/disclaimer/document.rb
disclaimer-0.0.4 app/models/disclaimer/document.rb
disclaimer-0.0.3 app/models/disclaimer/document.rb