Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rss feed of projects #115

Merged
merged 7 commits into from
Mar 20, 2015
Merged
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
38 changes: 24 additions & 14 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ProjectsController < ApplicationController
skip_before_filter :store_location, :only => [:join, :leave, :like, :dislike, :add_keyword, :delete_keyword ]
skip_before_action :verify_authenticity_token, :only => [:add_keyword, :delete_keyword ]
skip_load_and_authorize_resource :only => :old_archived
before_action :set_episode_id, :only => [:add_episode, :delete_episode]
before_action :load_episode

# GET /projects
def index
Expand All @@ -14,6 +14,16 @@ def index
@new= Project.current(@episode).active.order("created_at ASC").first(5)
end

# GET /projects/newest.rss

def newest
@newest = Project.current(@episode).active.includes(:episode_project_associations).
order('episodes_projects.created_at DESC').references(:episodes_projects).first(10)
respond_to do |format|
format.rss { render :layout => false }
end
end

# GET /projects/popular
def popular
@projects = Project.current(@episode).liked.order("likes_count DESC")
Expand Down Expand Up @@ -71,7 +81,7 @@ def create
@project.originator = current_user

if @project.save
redirect_to project_path(@episode, @project), notice: "Project was successfully created."
redirect_to project_path(@episode, @project), notice: 'Project was successfully created.'
else
render action: "new"
end
Expand All @@ -82,7 +92,7 @@ def update
if @project.update_attributes(project_params)
redirect_to project_path(@episode, @project)
else
render action: "edit"
render action: "edit"
end
end

Expand Down Expand Up @@ -139,7 +149,7 @@ def like
# PUT /projects/1/dislike
def dislike
@project.dislike! current_user

respond_to do |format|
format.html{ redirect_to project_path(@episode, @project), notice: "Aaww Snap! You don't love me anymore?" }
format.js { render :partial => "like_toggle" }
Expand Down Expand Up @@ -168,31 +178,31 @@ def delete_keyword

# PUT /projects/1/add_hackweek/1
def add_episode
unless @project.episodes.include? @subject
@project.episodes << @subject
unless @project.episodes.include? @episode
@project.episodes << @episode
end

redirect_to project_path(@episode, @project), notice: "Added hackweek #{@subject.name}"
redirect_to project_path(@episode, @project), notice: "Added hackweek #{@episode.name}"
end

# DELETE /projects/1/delete_hackweek/2
def delete_episode
@project.episodes.delete(@subject)
redirect_to project_path(@episode, @project), notice: "Removed hackweek #{@subject.name}"
@project.episodes.delete(@episode)
redirect_to project_path(nil, @project), notice: "Removed hackweek #{@episode.name}"
end

private

def project_params
params.require(:project).permit(:description, :title, :avatar)
end

def keyword_params
params.require(:keyword)
end
def set_episode_id
@subject = Episode.find(params[:episode_id])

def load_episode
@episode = Episode.find(params[:episode_id]) if params[:episode_id]
end

end
5 changes: 4 additions & 1 deletion app/models/episode.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Episode < ActiveRecord::Base
has_and_belongs_to_many :projects

has_many :episode_project_associations
has_many :projects, through: :episode_project_associations

validates :name, presence: true

def self.active
Expand Down
8 changes: 8 additions & 0 deletions app/models/episode_project_association.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class EpisodeProjectAssociation < ActiveRecord::Base
self.table_name = 'episodes_projects'

belongs_to :episode
belongs_to :project

validates :episode, presence: true
end
3 changes: 2 additions & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Project < ActiveRecord::Base

has_many :comments, as: :commentable, dependent: :destroy

has_and_belongs_to_many :episodes
has_many :episode_project_associations
has_many :episodes, through: :episode_project_associations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change the association type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need the intermediate object — to store created_at and sort by it.
This allows me to add existing project on top of the RSS feed, if it was just added in the episode.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


has_attached_file :avatar, styles: { thumb: '64x64>' }, default_url: :random_avatar
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
Expand Down
7 changes: 7 additions & 0 deletions app/views/projects/_tabs.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@
%span.hidden-xs
Archived
%i.fa.fa-archive.visible-xs

%li.pull-right
=link_to newest_projects_path(@episode, @project, format: :rss), title: 'New projects feed' do
%i.fa.fa-rss
%span
RSS

25 changes: 25 additions & 0 deletions app/views/projects/newest.rss.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
!!! XML
%rss(version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:atom="http://www.w3.org/2005/Atom")
%channel
%link #{ newest_projects_url format: :rss }
%atom:link(href="#{ newest_projects_url format: :rss }"
rel="self"
type="application/rss+xml")

-if @episode
%title Newest #{ @episode.name } projects
%description These are the newest projects for the #{ @episode.name }
-else
%title Newest projects (all Hackweeks)
%description These are the newest projects for all Hackweeks

-@newest.each do |project|
%item
%title #{ project.title }
%link #{ project_url(project) }
%description #{ project.description }
%author #{ project.originator.email } (#{ project.originator.name })
%pubDate #{ project.created_at.rfc822 }
%guid(isPermaLink='true') #{ project_url(project) }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ModelForEpisodesProjectsAssociation < ActiveRecord::Migration
def change
add_column :episodes_projects, :id, :primary_key
add_column :episodes_projects, :created_at, :datetime

# We will be ordering this field to populate RSS feed
add_index :episodes_projects, :created_at

# To make sure every association is there only once
add_index :episodes_projects, [:episode_id, :project_id], unique: true

# Composite index [A, B] can be used in searches for only A, but not for only B
add_index :episodes_projects, :project_id
end
end
87 changes: 46 additions & 41 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140910105916) do
ActiveRecord::Schema.define(version: 20150319172246) do

create_table "announcements", force: true do |t|
create_table "announcements", force: :cascade do |t|
t.string "title"
t.text "text", limit: 16777215
t.text "text"
t.integer "originator_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "comments", force: true do |t|
create_table "comments", force: :cascade do |t|
t.text "text"
t.integer "commentable_id"
t.string "commentable_type"
t.integer "commenter_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "enrollments", force: true do |t|
create_table "enrollments", force: :cascade do |t|
t.integer "announcement_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "episodes", force: true do |t|
create_table "episodes", force: :cascade do |t|
t.string "name"
t.date "start_date"
t.date "end_date"
Expand All @@ -46,43 +46,48 @@
t.boolean "active", default: false
end

create_table "episodes_projects", id: false, force: true do |t|
t.integer "episode_id"
t.integer "project_id"
create_table "episodes_projects", force: :cascade do |t|
t.integer "episode_id"
t.integer "project_id"
t.datetime "created_at"
end

create_table "keywords", force: true do |t|
t.text "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
add_index "episodes_projects", ["created_at"], name: "index_episodes_projects_on_created_at"
add_index "episodes_projects", ["episode_id", "project_id"], name: "index_episodes_projects_on_episode_id_and_project_id", unique: true
add_index "episodes_projects", ["project_id"], name: "index_episodes_projects_on_project_id"

create_table "keywords", force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "likes", force: true do |t|
create_table "likes", force: :cascade do |t|
t.integer "project_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "memberships", force: true do |t|
create_table "memberships", force: :cascade do |t|
t.integer "project_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "project_interests", force: true do |t|
create_table "project_interests", force: :cascade do |t|
t.integer "project_id"
t.integer "keyword_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "projects", force: true do |t|
t.text "title"
create_table "projects", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "originator_id"
t.integer "likes_count"
t.integer "memberships_count"
Expand All @@ -93,38 +98,38 @@
t.datetime "avatar_updated_at"
end

create_table "roles", force: true do |t|
create_table "roles", force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "roles_users", id: false, force: true do |t|
create_table "roles_users", id: false, force: :cascade do |t|
t.integer "role_id"
t.integer "user_id"
end

create_table "updates", force: true do |t|
create_table "updates", force: :cascade do |t|
t.text "text"
t.integer "author_id"
t.integer "project_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "user_interests", force: true do |t|
create_table "user_interests", force: :cascade do |t|
t.integer "user_id"
t.integer "keyword_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", force: true do |t|
create_table "users", force: :cascade do |t|
t.string "uid"
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
Expand All @@ -136,7 +141,7 @@
t.string "last_sign_in_ip"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end
Loading