Sha256: 21066cb3aae15661d05c6c29b8d52df91253751b8d2ea58e49cd9138b5f95515

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

require 'rails'
require 'active_record'
require 'action_view'
module Cable
    module ActsAsCable
      def self.included( base )
        base.send :extend, ClassMethods

      end

      module ClassMethods
        
        def acts_as_cable( reflection_options = {} )
          send :include, InstanceMethods
          send :include, ActionView::Helpers::TextHelper
          
          with_modules = []
          with_modules << reflection_options.delete(:with) if reflection_options.has_key?(:with)
          with_modules.flatten!
          
          has_one :menu, reflection_options.merge( :as => :cable_menuable )
          has_many :blocks, :as => :resource if with_modules.include? :blocks
          accepts_nested_attributes_for :menu
          
          self.cattr_accessor :default_template
          self.default_template = "default"
          
          yield self if block_given?
        end
        
        def template( template_name )
          self.default_template = (Cable.templates.include?( template_name.to_s )) ? template_name.to_s : :default.to_s
        end
        
      end
      
      module InstanceMethods
        
        def to_json_for_autocomplete
          displayable_string = first_displayable_attribute
          #TODO: find first string attr or look at sphinx index# if displayable_string.nil?
          { :label => "#{truncate(displayable_string, :length => 34)}" , :value => "#{(displayable_string)}", :type => self.class.name  }
        end
    
        def first_displayable_attribute
          displayable_attr = [ :title, :name, :first_name, :email, :email_address ].select{|attr| self.respond_to? attr }.first
          displayable_string = (displayable_attr.nil?) ? "Title not available" : self[displayable_attr]
        end
    
      end
      
    end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cable-0.9.17 lib/cable/acts_as_cable.rb
cable-0.9.16 lib/cable/acts_as_cable.rb
cable-0.9.15 lib/cable/acts_as_cable.rb
cable-0.9.14 lib/cable/acts_as_cable.rb