Skip to content

Commit

Permalink
Merge pull request #183 from robotmay/license-explore
Browse files Browse the repository at this point in the history
Add option for browsing by license
  • Loading branch information
robotmay committed Nov 3, 2013
2 parents 9b82687 + 7fb9767 commit a889248
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/licenses.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/licenses.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the licenses controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
9 changes: 8 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ class ApplicationController < ActionController::Base

before_filter :fetch_categories
def fetch_categories
@categories = Rails.cache.fetch([:categories, :list], expires_in: 5.minutes) do
@categories = Rails.cache.fetch([I18n.locale, :categories, :list], expires_in: 5.minutes) do
Category.order("name ASC").load
end
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
end

before_filter :google_analytics_identification
def google_analytics_identification
if $gabba.present?
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/licenses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class LicensesController < ApplicationController
respond_to :html

def show
@license = License.friendly.find(params[:id])
authorize! :read, @license

respond_with @license do |f|
f.html { redirect_to license_photographs_path(@license) }
end
end
end
19 changes: 14 additions & 5 deletions app/controllers/photographs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PhotographsController < ApplicationController
before_filter :set_parents
def set_parents
if params[:collection_id].present?
@collection = Collection.find(params[:collection_id])
@collection = Collection.friendly.find(params[:collection_id])
authorize! :read, @collection
end

Expand All @@ -25,22 +25,31 @@ def set_parents
end

if params[:category_id].present?
@category = Category.find(params[:category_id])
@category = Category.friendly.find(params[:category_id])
end

if params[:license_id].present?
@license = License.friendly.find(params[:license_id])
end
end

def index
if @collection.present?
case
when @collection.present?
@photographs = @collection.photographs
set_title(@collection.name)
hide_filters!
elsif @user.present?
when @user.present?
@photographs = @user.photographs
set_title(@user.name)
hide_filters!
elsif @category.present?
when @category.present?
@photographs = @category.photographs
set_title(@category.name)
when @license.present?
@photographs = @license.photographs
set_title(@license.name)
hide_filters!
else
@photographs = Photograph.all
end
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/licenses_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LicensesHelper
end
1 change: 1 addition & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def initialize(user)
alias_action :create, :read, :update, :destroy, to: :crud

can :read, Category
can :read, License
can :read, Photograph do |photograph|
photograph.visible?
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/license.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
class License < ActiveRecord::Base
extend FriendlyId

has_many :photographs

friendly_id :code, use: [:slugged, :finders]

validates :name, :code, presence: true
end
13 changes: 13 additions & 0 deletions app/views/shared/_nav.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ nav id="layout-nav" class="top-bar"
= link_to category.name, category_path(category.slug), active_on: category_photographs_path(category.slug), \
active_wrapper: :li

- if @licenses.present?
div class="row"
div class="large-12 columns"
label= t("nav.license")

div class="row"
- @licenses.each_slice((@licenses.size / 2) + 1) do |licenses|
div class="large-6 columns"
ul class="sub-nav"
- licenses.each do |license|
= link_to license.code, license_path(license), active_on: license_photographs_path(license), \
active_wrapper: :li

= link_to t("nav.recommended"), recommended_photographs_path, active_on: true, \
active_wrapper: :li

Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ en:
dashboard: "Dashboard"
seeking_feedback: "Photographs seeking feedback"
about: "About"
license: "Explore by License"

beta_notes:
title: "This is an early stage beta!"
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
resources :collections, only: [:index]
end

resources :licenses, only: [:show] do
resources :photographs, only: [:index]
end

resources :notifications, only: [:index, :show] do
collection do
get :mark_all_as_read
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20131103133641_add_slug_to_licenses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddSlugToLicenses < ActiveRecord::Migration
def change
add_column :licenses, :slug, :string
add_index :licenses, :slug
end
end
12 changes: 11 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ CREATE TABLE licenses (
name character varying(255),
code character varying(255),
created_at timestamp without time zone,
updated_at timestamp without time zone
updated_at timestamp without time zone,
slug character varying(255)
);


Expand Down Expand Up @@ -1201,6 +1202,13 @@ CREATE INDEX index_followings_on_followee_id ON followings USING btree (followee
CREATE INDEX index_followings_on_follower_id ON followings USING btree (follower_id);


--
-- Name: index_licenses_on_slug; Type: INDEX; Schema: public; Owner: -; Tablespace:
--

CREATE INDEX index_licenses_on_slug ON licenses USING btree (slug);


--
-- Name: index_metadata_on_photograph_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
Expand Down Expand Up @@ -1567,3 +1575,5 @@ INSERT INTO schema_migrations (version) VALUES ('20130826172509');
INSERT INTO schema_migrations (version) VALUES ('20130830080641');

INSERT INTO schema_migrations (version) VALUES ('20131022130051');

INSERT INTO schema_migrations (version) VALUES ('20131103133641');
4 changes: 2 additions & 2 deletions lib/tasks/iso.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace :iso do
namespace :maintenance do
task :create_stories => :environment do
Notification.find_each(&:create_story)
task :repair_licenses => :environment do
License.find_each(&:save)
end

task :repair_image_permissions => :environment do
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/licenses_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'spec_helper'

describe LicensesController do
describe "GET show" do
let(:license) { License.make }

before { License.stub_chain(:friendly, :find) { license } }
before { get :show, id: 1 }

it "redirects to photographs path" do
response.should redirect_to license_photographs_path(license)
end
end
end
15 changes: 15 additions & 0 deletions spec/helpers/licenses_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the LicensesHelper. For example:
#
# describe LicensesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe LicensesHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
6 changes: 6 additions & 0 deletions spec/support/blueprints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
follower
end

License.blueprint do
name { "Attribution" }
code { "CC-BY" }
slug { "cc-by" }
end

Metadata.blueprint do
photograph
title { Faker::Lorem.sentence }
Expand Down

0 comments on commit a889248

Please sign in to comment.