Sha256: ef6263c939ffd3f9968c8e1a5fb5bb1685af4d2d87b6adf84e683aa93f86c42a

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

module TrackChanges
  # A Result instance is yielded by ClassMethods#track_changes_to.
  #
  # Example:
  #
  #   track_changes_to :post do |result|
  #     result.post         # => The instance variable in the current_controler
  #                         #    given by the :post option.
  #     result.controller   # => The instance of the current controller
  #     result.current_user # => The current controllers @current_user
  #     result.some_method  # => Call some_method on the current controller instance
  #   end
  class Result
    attr_accessor :changes, :controller

    def initialize(model_name, controller, changes)
      @changes = changes
      @controller = controller

      self.class.send(:define_method, model_name) do
        @controller.instance_variable_get("@#{model_name}")
      end
    end

    # Pass all other methods to the controller.
    def method_missing(method_sym, *args)
      controller.send(method_sym, *args)
    end

    # A convienence method that returns the <tt>@current_user</tt>
    # in the current controller.
    def current_user
      user = controller.instance_variable_get("@current_user")
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
smajn-track_changes-0.2.0 lib/track_changes/result.rb
smajn-track_changes-0.2.1 lib/track_changes/result.rb
smajn-track_changes-0.3.0 lib/track_changes/result.rb
track_changes-0.5.0 lib/track_changes/result.rb
track_changes-0.4.1 lib/track_changes/result.rb
track_changes-0.4.0 lib/track_changes/result.rb
track_changes-0.3.2 lib/track_changes/result.rb
track_changes-0.3.0 lib/track_changes/result.rb