Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert May committed Dec 8, 2013
2 parents 15c451d + eef1d45 commit 8e9a5dd
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 32 deletions.
42 changes: 41 additions & 1 deletion app/assets/stylesheets/photographs.css.sass
Expand Up @@ -60,12 +60,52 @@
.button
margin: 0

&:hover .interactions
&:hover .interactions, &:hover .photo-navigation
opacity: 1

.actual-interactions
text-align: right

.photo-navigation
opacity: 0
transition: opacity .25s ease-in-out
-moz-transition: opacity .25s ease-in-out
-webkit-transition: opacity .25s ease-in-out

.previous, .next
position: absolute
display: block
margin: 20px

a
display: block
width: 20px
height: 20px
padding: 40px
background:
repeat: no-repeat
position: 50%
color: rgba(0,0,0,.4)

&:hover
background-color: rgba(0,0,0,.6)

.previous
left: 0
top: 40%
text-align: left
text-indent: -99999px
a
background-image: image-url('glyphicons_inverted/glyphicons_210_left_arrow.png')

.next
right: 0
top: 40%
text-align: right
text-indent: 99999px
a
background-image: image-url('glyphicons_inverted/glyphicons_211_right_arrow.png')

.stats
@include stats("glyphicons_halflings")
text-align: left
Expand Down
75 changes: 45 additions & 30 deletions app/controllers/application_controller.rb
Expand Up @@ -4,40 +4,12 @@ class ApplicationController < ActionController::Base
helper_method :set_title, :hide_filters?, :sharing_mode

attr_accessor :sharing_mode

before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || (user_signed_in? ? current_user.locale : I18n.default_locale)
end

before_filter :fetch_categories
def fetch_categories
@categories = Rails.cache.fetch([I18n.locale, :categories, :list], expires_in: 5.minutes) do
Category.all.sort_by(&:name)
end

# Handle a cache failure here, as it will impact entire site
rescue Exception
@categories = Category.order("name ASC")
end

before_filter :fetch_licenses
def fetch_licenses
@licenses = Rails.cache.fetch([I18n.locale, :licenses, :list], expires_in: 5.minutes) do
License.order("id ASC").load
end

# Handle a cache failure here, as it will impact entire site
rescue Exception
@licenses = License.order("id ASC")
end

before_filter :google_analytics_identification
def google_analytics_identification
if $gabba.present?
$gabba.identify_user(cookies[:__utma], cookies[:__utmz])
end
end
after_filter :track_last_viewed_photographs

rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, alert: exception.message
Expand Down Expand Up @@ -87,4 +59,47 @@ def hide_filters!
def enable_sharing_mode
self.sharing_mode = true
end

def set_locale
I18n.locale = params[:locale] || (user_signed_in? ? current_user.locale : I18n.default_locale)
end

def fetch_categories
@categories = Rails.cache.fetch([I18n.locale, :categories, :list], expires_in: 5.minutes) do
Category.all.sort_by(&:name)
end

# Handle a cache failure here, as it will impact entire site
rescue Exception
@categories = Category.order("name ASC")
end

def fetch_licenses
@licenses = Rails.cache.fetch([I18n.locale, :licenses, :list], expires_in: 5.minutes) do
License.order("id ASC").load
end

# Handle a cache failure here, as it will impact entire site
rescue Exception
@licenses = License.order("id ASC")
end

def google_analytics_identification
if $gabba.present?
$gabba.identify_user(cookies[:__utma], cookies[:__utmz])
end
end

def track_last_viewed_photographs
case
# When a collection is being viewed, store its photo ids
when @collection.present?
flash[:last_viewed_photograph_ids] = @collection.photographs.visible.pluck(:id).uniq
# If viewing a photo and previously viewed a set of photos, keep the photo ids
when flash[:last_viewed_photograph_ids].present? && @photograph.present?
if flash[:last_viewed_photograph_ids].include?(@photograph.id)
flash.keep
end
end
end
end
28 changes: 28 additions & 0 deletions app/helpers/photographs_helper.rb
Expand Up @@ -158,4 +158,32 @@ def metadata_typeahead_values(attr = nil)
end
end
end

def last_viewed_photograph_ids
flash[:last_viewed_photograph_ids]
end

def last_viewed_photographs
if last_viewed_photograph_ids.present?
Photograph.view_for(current_user).where(id: flash[:last_viewed_photograph_ids])
end
end

def next_photo_id(photograph, photograph_ids)
if photograph_ids.present? && photograph_ids.size > 0
index = photograph_ids.index(photograph.id)
photograph_ids[(index || 0) + 1]
end
end

def previous_photo_id(photograph, photograph_ids)
if photograph_ids.present? && photograph_ids.size > 0
index = photograph_ids.index(photograph.id)
if index == 0
nil
else
photograph_ids[(index || 0) - 1]
end
end
end
end
11 changes: 11 additions & 0 deletions app/views/photographs/_interactions.html.slim
Expand Up @@ -32,3 +32,14 @@ div class="interactions"
= link_to photograph_favourites_path(photograph), method: :post, class: "button small favourite secondary", "data-remote" => true do
| &nbsp;

div class="photo-navigation"
div class="inner"
- previous_id = previous_photo_id(photograph, last_viewed_photograph_ids)
- if previous_id.present?
span class="previous"
= link_to t("previous"), photograph_path(id: previous_id)

- next_id = next_photo_id(photograph, last_viewed_photograph_ids)
- if next_id.present?
span class="next"
= link_to t("next"), photograph_path(id: next_id)
2 changes: 2 additions & 0 deletions config/locales/en.yml
Expand Up @@ -29,6 +29,8 @@ en:
twitter_account: "@photographer_io"
github: "GitHub"
privacy_policy: "https://www.iubenda.com/privacy-policy/288645"
previous: "Previous"
next: "Next"

locales:
en: "English"
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/application_controller_spec.rb
Expand Up @@ -8,7 +8,7 @@
before { Category.stub(:where) { [] } }

it "cache failure shouldn't break the site" do
instance.fetch_categories.should eq([])
instance.send(:fetch_categories).should eq([])
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/controllers/collections_controller_spec.rb
Expand Up @@ -9,6 +9,7 @@
let(:photographs) { [Photograph.make] }

before do
controller.stub(:track_last_viewed_photographs) { nil }
collection.stub(:id) { 1 }
collection.stub_chain(:photographs, :visible, :page) { photographs }
Collection.stub(:find) { collection }
Expand Down

0 comments on commit 8e9a5dd

Please sign in to comment.