Skip to content

Api changes in version 3

Vinicius Ferreira Negrisolo edited this page May 9, 2016 · 29 revisions

Plurals

-v 2.x

class Controller
  expose(:person)
end

-v 3.x

In version 3 plural helpers are not automatically generated. Now collections must be explicitly exposed.

class Controller
  expose(:people, -> { Person.all })
  expose(:person)
end

Expose options

expose(name, options = {}, &block)

-v 2.x

  • Possible options
:ancestor
another exposure to scope from
:model
model to use for that exposure
:params
method to call on the controller to get a params hash
:finder
method used to find the record
:finder_parameter
attribute containing the finder method's unique identifier

-v 3.x

  • Possible options
:fetch
:id
:find
:build
:build_params
:scope
:model
:decorate
  • Use :from in place of :ancestor
  • Use :build_params in place of :params

Instead of using :finder and :finder_parameter like this:

class PeopleController < ApplicationController
  expose(:people, finder: :find_by_slug, finder_parameter: :slug)
end

This is now equivalent to:

class PeopleController < ApplicationController
  expose :people, find_by: :slug, id: :slug
end

Strategies

-v 2.x

class CurrentUserStrategy < DecentExposure::Strategy
  delegate :current_user, :to => :controller

  def resource
    instance = model.find(params[:id])
    if current_user != instance.user
      raise ActiveRecord::RecordNotFound
    end
    instance
  end
end

class PostsController < ApplicationController
  expose :post, strategy: CurrentUserStrategy
end

-v 3.x

class PostsController < ApplicationController
  exposure_config :current_user_strategy, find: ->(id, scope){
    post = scope.find(id)
    if current_user != post.user
      raise ActiveRecord::RecordNotFound
    end
    post
  }

expose :thing, find: ->(id, scope){ scope.find(id) }

  expose :post, with: :current_user_strategy
end