Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Using the release ID instead of the ref ID in URLs #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ gem 'jbuilder', '~> 1.2'

gem 'yajl-ruby', require: 'yajl'

gem 'prismic.io', '~> 1.0.0.rc8', require: 'prismic'
gem 'prismic.io', '~> 1.0.0.rc9', require: 'prismic'

group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ GEM
minitest (4.7.5)
multi_json (1.7.9)
polyglot (0.3.3)
prismic.io (1.0.0.rc8)
prismic.io (1.0.0.rc9)
rack (1.5.2)
rack-test (0.6.2)
rack (>= 1.0)
Expand Down Expand Up @@ -117,7 +117,7 @@ DEPENDENCIES
coffee-rails (~> 4.0.0)
jbuilder (~> 1.2)
jquery-rails
prismic.io (~> 1.0.0.rc8)
prismic.io (~> 1.0.0.rc9)
rails (= 4.0.0)
rails_12factor
sass-rails (~> 4.0.0)
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/prismic_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ def redirect_to_signin
end

# Setting @ref as the actual ref id being queried, even if it's the master ref.
# To be used to call the API, for instance: api.form('everything').submit(ref)
# To be used to call the API, for instance: api.form('everything').submit(ref).
# Note that this is the ref ID (the "ref" field of a ref), the one to be used to query.
def ref
@ref ||= maybe_ref || api.master_ref.ref
@ref ||= (params[:ref].blank? ? api.master_ref.ref : api.refs.select{|_, ref| ref.id == params[:ref]}.values[0].ref)
end

# Setting @maybe_ref as the ref id being queried, or nil if it is the master ref.
# To be used where you want nothing if on master, but something if on another release.
# For instance:
# * you can use it to call Rails routes: document_path(ref: maybe_ref), which will add "?ref=refid" as a param, but only when needed.
# * you can pass it to your link_resolver method, which will use it accordingly.
# Note that this is the release ID (the "is" field of a ref), the one to be used in your URL.
def maybe_ref
@maybe_ref ||= (params[:ref].blank? ? nil : params[:ref])
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/prismic_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def api

# Return the actual used reference
def ref
@ref ||= maybe_ref || api.master_ref.ref
@ref ||= (params[:ref].blank? ? api.master_ref.ref : api.refs.select{|_, ref| ref.id == params[:ref]}.values[0].ref)
end

# Return the set reference
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<option value="" <% if ref == api.master %>selected="selected"<% end %>>As currently seen by guest visitors</option>
<optgroup label="Or preview the website in a future release:">
<% api.refs.reject{|_,r| r.master? }.each do |_, r| %>
<option value="<%= r.ref %>" <% if ref == r.ref %>selected="selected"<% end %>>
<option value="<%= r.id %>" <% if params[:ref] == r.id %>selected="selected"<% end %>>
As <%= r.label %> <%= r.scheduled_at.try(:strftime, "%Y-%m-%d") %>
</option>
<% end %>
Expand Down