Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit

Permalink
Add player view for collection items
Browse files Browse the repository at this point in the history
  • Loading branch information
wilfriedE committed Nov 17, 2017
1 parent a3f6017 commit fa066e0
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 11 deletions.
29 changes: 29 additions & 0 deletions app/controllers/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def search_collectible
respond_to :js
end

def player
@collection_player = true
@collection = Collection.find(params[:id])
authorize @collection, :show?
@collection_items = @collection.collection_items.page(params[:page]).per(1)
render_player_view @collection_items.first
end

def list_collectibles
@collectible_type = params[:collectible_type]
@collectibles = Lesson.search(active_version_name_or_active_version_description_cont: params[:q]).result(distinct: true) if @collectible_type == "Lesson"
Expand Down Expand Up @@ -126,4 +134,25 @@ def valid_collection_items_count?(collection)
collection.errors.add(:base, "A collection must have at least two Collection Items") unless valid
valid
end

def render_player_view(collection_item)
render_lesson_viewer collection_item.collectible if collection_item.lesson?
render_lesson_version_viewer collection_item.collectible if collection_item.lesson_version?
render_collection_viewer collection_item.collectible if collection_item.collection?
end

def render_lesson_viewer(lesson)
@lesson = lesson
render "lessons/show"
end

def render_lesson_version_viewer(lesson_version)
@lesson_version = lesson_version
render "lesson_versions/show"
end

def render_collection_viewer(collection)
@collection = collection
render "collections/show"
end
end
9 changes: 9 additions & 0 deletions app/views/collections/_player_pagination.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% prev_link = path_to_prev_page(@collection_items) %>
<% next_link = path_to_next_page(@collection_items) %>
<div class="float-left col-6 justify-content-start">
<%= prev_link.present? ? link_to( "Previous", prev_link, class: "btn btn-outline-secondary") : ""%>
</div>

<div class="float-right col-6 justify-content-end text-right">
<%= next_link.present? ? link_to( "Next", next_link, class: "btn btn-outline-primary") : "" %>
</div>
18 changes: 14 additions & 4 deletions app/views/collections/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
<div class="card border border-light">
<div class="card-body bg-light">
<h4 class="card-title text-primary"><%= @collection.name %></h4>
<p class="card-text text-muted"><%= @collection.description %></p>
<p class="card-text"><small class="text-muted">Contributed by: <%= @collection.creator.nickname %></small></p>
<div class="card-body bg-light clearfix">
<div class="float-left col-8 justify-content-start">
<h4 class="card-title text-primary"><%= @collection.name %></h4>
<p class="card-text text-muted"><%= @collection.description %></p>
<p class="card-text"><small class="text-muted">Contributed by: <%= @collection.creator.nickname %></small></p>
</div>
<div class="float-right col-4 d-flex align-content-center justify-content-end flex-wrap">
<%= link_to "Start collection", player_collection_path(@collection, page: 1), class: "btn btn-outline-primary flex-item" %>
</div>
</div>
<% if @collection_player %>
<div class="card-body bg-light clearfix">
<%= render "collections/player_pagination" %>
</div>
<% end %>
</div>

<div class="w-100 pt-2 border border-light rounded-bottom pl-4 pr-4">
Expand Down
15 changes: 11 additions & 4 deletions app/views/lesson_versions/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<div class="card border border-light">
<div class="card-body bg-light">
<h4 class="card-title text-primary"><%= @lesson_version.name %></h4>
<p class="card-text text-muted"><%= @lesson_version.description %></p>
<p class="card-text"><small class="text-muted">Contributed by: <%= @lesson_version.creator.nickname %></small></p>
<div class="card-body bg-light clearfix">
<div class="float-left col-12 justify-content-start">
<h4 class="card-title text-primary"><%= @lesson_version.name %></h4>
<p class="card-text text-muted"><%= @lesson_version.description %></p>
<p class="card-text"><small class="text-muted">Contributed by: <%= @lesson_version.creator.nickname %></small></p>
</div>
</div>
<% if @collection_player %>
<div class="card-body bg-light clearfix">
<%= render "collections/player_pagination" %>
</div>
<% end %>
</div>

<div class="w-100 pt-2 border border-light rounded-bottom pl-4 pr-4">
Expand Down
9 changes: 7 additions & 2 deletions app/views/lessons/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<div class="card border border-light">
<div class="card-body bg-light clearfix">
<div class="float-left">
<div class="float-left col-8 justify-content-start">
<h2 class="card-title text-primary"><%= @lesson.name %></h2>
<i class="card-text text-muted"><%= @lesson.description %></i>
</div>
<div class="float-right d-flex align-content-center flex-wrap">
<div class="float-right col-4 d-flex align-content-center justify-content-end flex-wrap">
<%= link_to "Propose Lesson Update", propose_update_lesson_path(@lesson), class: "btn btn-outline-primary flex-item" %>
</div>
</div>
<% if @collection_player %>
<div class="card-body bg-light clearfix">
<%= render "collections/player_pagination" %>
</div>
<% end %>
</div>

<div class="w-100 pt-2 border border-light rounded-bottom pl-4 pr-4">
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
member do
delete :remove_collection_item, path: "remove-collection-item/:collection_item_id"
put :collection_approval, path: "collection-approval", as: :approval
get :player, path: "player/(:page)"
end
end

Expand Down
37 changes: 37 additions & 0 deletions spec/controllers/collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,43 @@
end
end

describe "GET #player" do
let(:collection) { create(:collection) }
login_user

before(:each) do
collection.approved!
create(:collection_item, collection: collection, collectible: create(:lesson), position: 1)
create(:collection_item, collection: collection, collectible: create(:lesson_version), position: 2)
create(:collection_item, collection: collection, collectible: create(:collection), position: 3)
end

after(:each) do
CollectionItem.destroy_all
end

it 'renders lesson show view when collectible is a lesson' do
get :player, params: { id: collection.id }

expect(collection.collection_items.first).to be_lesson
expect(response).to render_template("lessons/show")
end

it 'renders lesson version show view when collectible is a lesson version' do
get :player, params: { id: collection.id, page: 2 }

expect(collection.collection_items.second).to be_lesson_version
expect(response).to render_template("lesson_versions/show")
end

it 'renders collection show view when collectible is a collection' do
get :player, params: { id: collection.id, page: 3 }

expect(collection.collection_items.third).to be_collection
expect(response).to render_template("collections/show")
end
end

describe "GET #list_collectibles" do
context "unauthenticated user" do
it 'does nothing' do
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/collection_items.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :collection_item do
sequence(:position)
collection
association :collection, factory: :collection
association :collectible, factory: :lesson
end
end

0 comments on commit fa066e0

Please sign in to comment.