Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request env #8

Closed
blakechambers opened this issue Mar 26, 2015 · 0 comments
Closed

Request env #8

blakechambers opened this issue Mar 26, 2015 · 0 comments

Comments

@blakechambers
Copy link
Owner

Matterhorn needs a request_env to store it's request logic. Currently there isn't good separation of concerns between the logic for generating the links inclusions and the actual display of those objects.

This is needed prior to finishing the code for inclusions, which currently don't track the request env at all. Inclusions will need to have access to request_env in order to check for authentication visibility... e.g. if you request an inclusion that requires authentication, the inclusion will need to decide how to handle that (raise a 403, ignore request).

This would include:

  • the current_user for the request
  • the primary resource/collection
  • link objects available to the request.
  • inclusions requested.

In your api app, most code bases should setup the following code:

# app/controllers/application_controller.rb
class ApplicationController < Matterhorn::Base
  include AuthenticationCode # this is custom to you're app

  helper_method :current_user
  add_env       :current_user

protected ###################################

  def current_user
    @current_user ||= begin
      User.where(id: session[:user_id]).first
    end
  end

end

Now within requests:

# app/controllers/posts_controller.rb

# matterhorn index methods should handle the index 
# methods for you, this is just to demonstrate that the
#  request_env is available as an instance method.
def index
  request_env[:current_user] # == current_user
end

When the request_env is called it should load each of the methods into a memoized hash. If it is not called, current_user shouldn't be built.

# builder_support
def build_json
  # this should merge controller.request_env into the base object, to be mixed into options for scoped collection/resource.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant