-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from openjournals/collections
User-curated collections
- Loading branch information
Showing
41 changed files
with
566 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# 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/ | ||
|
||
$ -> | ||
$(".collection-idea-button").click -> | ||
this.remove() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
class CollectionsController < ApplicationController | ||
before_filter :require_user, :except => [ :show ] | ||
|
||
def new | ||
@collection = Collection.new | ||
|
||
if @idea = Idea.find_by_sha(params[:idea_id]) | ||
@collection.ideas << @idea | ||
end | ||
end | ||
|
||
def show | ||
@collection = Collection.find_by_sha(params[:id]) | ||
|
||
respond_to do |format| | ||
format.atom { render :atom => @collection, :include => [ :ideas ] } | ||
format.json { render :json => @collection, :include => [ :ideas ] } | ||
format.html | ||
end | ||
end | ||
|
||
def create | ||
@collection = Collection.new(collection_params) | ||
@collection.user = current_user | ||
|
||
if @collection.save | ||
set_ideas | ||
redirect_to collection_path(@collection), :notice => "Collection created" | ||
end | ||
end | ||
|
||
def edit | ||
@collection = Collection.find_by_sha(params[:id]) | ||
redirect_to collections_path, :warning => "Collection not found" unless @collection | ||
|
||
# Redirect if not owner | ||
redirect_to collection_path(@collection) unless @collection.owner == current_user | ||
end | ||
|
||
def update | ||
@collection = Collection.find_by_sha(params[:id]) | ||
redirect_to collections_path, :warning => "Collection not found" unless @collection | ||
redirect_to collection_path(@collection) unless @collection.owner == current_user | ||
|
||
set_ideas | ||
|
||
redirect_to collection_path(@collection), :notice => "Collection updated" | ||
end | ||
|
||
def add_idea | ||
@collection = Collection.find_by_sha(params[:id]) | ||
@idea = Idea.find_by_sha(params[:idea_id]) | ||
@collection.ideas << @idea unless @collection.ideas.include?(@idea) | ||
redirect_to collection_path(@collection), :notice => "Idea added" | ||
end | ||
|
||
def destroy | ||
@collection = Collection.find_by_sha(params[:id]) | ||
redirect_to collections_path, :warning => "Collection not found" unless @collection | ||
redirect_to collection_path(@collection) unless @collection.owner == current_user | ||
|
||
if @collection.destroy | ||
redirect_to collections_path, :warning => "Collection deleted" | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_ideas | ||
@collection.collection_ideas.destroy_all | ||
|
||
# This will be empty if there are no ideas | ||
return true unless params['collection']['ideas'] | ||
|
||
params['collection']['ideas'].each do |_, idea_sha| | ||
@idea = Idea.find_by_sha(idea_sha) | ||
@collection.ideas << @idea if @idea | ||
end | ||
end | ||
|
||
def collection_params | ||
params.require(:collection).permit(:name, :description) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Collection < ActiveRecord::Base | ||
belongs_to :user | ||
has_many :collection_ideas, :dependent => :destroy | ||
has_many :ideas, :through => :collection_ideas | ||
|
||
before_create :set_sha | ||
|
||
def to_param | ||
sha | ||
end | ||
|
||
def owner | ||
user | ||
end | ||
|
||
private | ||
|
||
def set_sha | ||
self.sha = SecureRandom.hex | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class CollectionIdea < ActiveRecord::Base | ||
belongs_to :collection | ||
belongs_to :idea | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<%= render 'layouts/errors', :object => @collection %> | ||
|
||
<%= form_for @collection, :html => {:data => {:toggle => "validator"}} do |f| %> | ||
<div class="form-group"> | ||
<%= f.label :name, :class => "control-label" %> | ||
<%= f.text_field :name, :class => "form-control", "placeholder" => "Enter your collection name", :required => "" %> | ||
<div class="help-block with-errors"></div> | ||
</div> | ||
|
||
<%= render :partial => "ideas", :locals => { :form => f } %> | ||
|
||
<%= f.button :class => 'btn btn-default' %> | ||
<% end %> | ||
|
||
<%= button_to "Delete Collection", collection_path(:id => @collection.to_param), :method => :delete, :class => 'btn btn-danger', :style => "width:140px;margin-top:5px", :data => {:confirm => 'Are you sure?'} unless @collection.new_record? %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<div class="form-group"> | ||
<%= form.label :ideas, :class => "control-label" %> | ||
|
||
<% if @collection.ideas.any? %> | ||
<div class="collection-ideas"> | ||
<% @collection.ideas.each_with_index do |idea, index| %> | ||
<button type="button" class="btn btn-default btn-sm collection-idea-button"> | ||
<%= idea.title %> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> | ||
<%= form.hidden_field "ideas[#{index}]", :value => idea.sha %> | ||
</button> | ||
<% end %> | ||
</div> | ||
<% else %> | ||
<p>This collection has no ideas</p> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<div class="page-header"> | ||
<h1>Edit Collection</h1> | ||
</div> | ||
|
||
<div class="row form"> | ||
<div class="col-sm-8"> | ||
<%= render :partial => 'shared/flashes', :locals => { :flash => flash } %> | ||
<%= render :partial => "users/email" unless current_user.email? %> | ||
<%= render :partial => "form" %> | ||
</div> | ||
<div class="col-sm-3 col-sm-offset-1"> | ||
<div class="login-block"> | ||
<p class="name"><%= current_user.name %> · <%= link_to "Sign out", signout_path %></p> | ||
</div> | ||
|
||
<div class="helper-block"> | ||
<h5>About Collections</h5> | ||
<p>Collections are a way for you to group ideas and share these curated collections with others.</p> | ||
</div> | ||
|
||
<div class="helper-block"> | ||
<h5>Formatting</h5> | ||
<p>You can use Markdown to format your ideas. Read more about the supported syntax <a href="https://help.github.com/articles/github-flavored-markdown/" target="_blank">here</a>.</p> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<div class="page-header"> | ||
<h1>Create a new collection</h1> | ||
</div> | ||
|
||
<div class="row form"> | ||
<div class="col-sm-8"> | ||
<%= render :partial => 'shared/flashes', :locals => { :flash => flash } %> | ||
<%= render :partial => "users/email" unless current_user.email? %> | ||
<%= render :partial => "form" %> | ||
</div> | ||
<div class="col-sm-3 col-sm-offset-1"> | ||
<div class="login-block"> | ||
<p class="name"><%= current_user.name %> · <%= link_to "Sign out", signout_path %></p> | ||
</div> | ||
|
||
<div class="helper-block"> | ||
<h5>About Collections</h5> | ||
<p>Collections are a way for you to group ideas and share these curated collections with others.</p> | ||
</div> | ||
|
||
<div class="helper-block"> | ||
<h5>Formatting</h5> | ||
<p>You can use Markdown to format your ideas. Read more about the supported syntax <a href="https://help.github.com/articles/github-flavored-markdown/" target="_blank">here</a>.</p> | ||
</div> | ||
|
||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
atom_feed do |feed| | ||
feed.title("Journal of Brief Ideas: Collection #{@collection.name}") | ||
feed.updated(@collection.collection_ideas.last.created_at) if @collection.ideas.length > 0 | ||
|
||
@collection.ideas.each do |idea| | ||
feed.entry(idea) do |entry| | ||
entry.title(idea.title) | ||
entry.doi(idea.doi) | ||
entry.content(idea.body, type: 'html') | ||
|
||
entry.author do |author| | ||
author.name(idea.user.name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<div class="page-header"> | ||
<h1><%= @collection.name %></h1> | ||
</div> | ||
|
||
<div class="row form"> | ||
<div class="col-sm-8"> | ||
<%= render :partial => 'shared/flashes', :locals => { :flash => flash } %> | ||
<p class="lead"><em>Curated by <%= link_to @collection.user.nice_name, user_path(@collection.user) %></em></p> | ||
|
||
<% if @collection.ideas.any? %> | ||
<%= render :partial => "ideas/idea", :collection => @collection.ideas.visible %> | ||
<% else %> | ||
<p>This collection doesn't have any ideas.</p> | ||
<% end %> | ||
|
||
</div> | ||
<div class="col-sm-3 col-sm-offset-1"> | ||
<%= render :partial => 'sessions/login' %> | ||
|
||
<div class="help-block"> | ||
<% if current_user && @collection.owner == current_user %> | ||
<h5>Manage collection</h5> | ||
<p><%= link_to "Edit collection", edit_collection_path(@collection) %></p> | ||
<% end %> | ||
|
||
<h5>Subscribe</h5> | ||
<p><%= image_tag "feed-icon-14x14.png" %> · <%= link_to "Subscribe to this collection", collection_path(@collection, :format => :atom) %></p> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.