Sha256: 1168792d807f6f311df3faa9f290e069a4994c3bfda8784a3da64221a4070fda

Contents?: true

Size: 1.33 KB

Versions: 12

Compression:

Stored size: 1.33 KB

Contents

# Nested and Polymorphic Resource Helpers
#
module Resourcelogic
  module Singleton
    def self.included(klass)
      klass.class_eval do
        add_acts_as_resource_module(Methods)
      end
    end
    
    module Methods
      def object
        return @object if defined?(@object)
        
        if singleton?
          if !parent? && respond_to?("current_#{model_name}", true)
            @object = send("current_#{model_name}")
          elsif parent? && parent_object.send(model_name)
            @object = parent_object.send(model_name)
          else
            super
          end
        else
          super
        end
      end
      
      def build_object
        if singleton? && parent?
          scope.send("build_#{model_name}")
        else
          super
        end
      end
      
      def scope
        if singleton? && parent?
          parent_object
        else
          super
        end
      end
      
      # Route alises can only be used for singleton, like account => user. Otherwise the urligence wont work because there is no account model.
      def object_url_parts(action = nil, *alternate_object_or_params)
        singleton? ? ([action] + contexts_url_parts + [route_name]) : super
      end
      
      # Override me with true to make singleton
      def singleton?
        false
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
binarylogic-resourcelogic-0.0.11 lib/resourcelogic/singleton.rb
binarylogic-resourcelogic-0.0.12 lib/resourcelogic/singleton.rb
binarylogic-resourcelogic-0.12.0 lib/resourcelogic/singleton.rb
binarylogic-resourcelogic-0.12.1 lib/resourcelogic/singleton.rb
binarylogic-resourcelogic-0.12.2 lib/resourcelogic/singleton.rb
resourcelogic-0.0.11 lib/resourcelogic/singleton.rb
resourcelogic-0.0.12 lib/resourcelogic/singleton.rb
resourcelogic-0.12.0 lib/resourcelogic/singleton.rb
resourcelogic-0.12.1 lib/resourcelogic/singleton.rb
resourcelogic-0.12.2 lib/resourcelogic/singleton.rb
resourcelogic-0.11.0 lib/resourcelogic/singleton.rb
resourcelogic-0.10.0 lib/resourcelogic/singleton.rb