Sha256: 61f7e563ad266d00eef5f30b1cc38bbb809c2900a368a1a9bbab7430f0c107e6

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

class Auth::Behavior::Base
  class_inheritable_array :migrations
  read_inheritable_attribute(:migrations) || write_inheritable_attribute(:migrations, [])
  
  class << self
    def apply_to_controllers
      # Add additional methods to ApplicationController (or whatever controller Sparkly is told to use)
      Auth.base_controller.send(:include, "#{name}::ControllerExtensions".constantize)
      # why this doesn't work in cuke?
  #    if (container = self.class).const_defined?(:ControllerExtensions)
  #      Auth.base_controller.send(:include, container.const_get(:ControllerExtensions))
  #    end
    #rescue NameError
    end
  end
  
  def apply_to(model)
    track_behavior(model.target) do
      apply_to_accounts(model)
      apply_to_passwords(Password)
    end
  end

  def apply_to_passwords(password_model)
    raise NotImplementedError, "Be sure to override #apply_to_passwords(passwords_model) in your Auth Behavior"
  end
  
  def apply_to_accounts(model_config)
    raise NotImplementedError, "Be sure to override #apply_to_accounts(model_config) in your Auth Behavior"
  end
  
  private
  def track_behavior(model)
    if !behavior_tracked?(model)
      track_behavior!(model)
      yield
    end
  end  

  def behavior_tracked?(model)
    behavior_tracker(model).include? behavior_name  
  end
    
  def track_behavior!(model)
    behavior_tracker(model) << behavior_name
  end
  
  def behavior_tracker(model)
    model.instance_variable_get("@__behavior_tracker") || model.instance_variable_set("@__behavior_tracker", [])
  end
  
  def behavior_name
    self.class.name
  end

  public
  class << self
    # Declares a migration template for a behavior. If sourcedir is given, it will be used as the location
    # in which to find the template.
    def migration(filename)
      migrations << filename unless migrations.include?(filename)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sparkly-auth-1.0.2 lib/auth/behavior/base.rb
sparkly-auth-1.0.1 lib/auth/behavior/base.rb
sparkly-auth-1.0.0 lib/auth/behavior/base.rb