Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ajax collaborators

  • Loading branch information...
commit 9b435928a059a396356197e5d83a8996efec7d3e 1 parent c4e637c
@kash5000 authored
View
3  app/assets/javascripts/links.js.coffee
@@ -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://jashkenas.github.com/coffee-script/
View
3  app/assets/stylesheets/links.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the links controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
1  app/controllers/collaborators_controller.rb
@@ -14,6 +14,7 @@ def create
def show
@collaborator = Collaborator.find(params[:id])
@posts = @collaborator.post_list
+ @projects = @collaborator.projects
end
def index
View
30 app/controllers/links_controller.rb
@@ -0,0 +1,30 @@
+class LinksController < ApplicationController
+ def create
+ @project = Project.find(params[:link][:project_id])
+ @collaborator = Collaborator.find_by_email(params[:link][:collaborator])
+
+
+ if !@collaborator
+ else
+ @project.add_collaborator!(@collaborator)
+ flash[:success] = "Collaborator Added"
+ respond_to do |format|
+ format.html {redirect_to @project}
+ format.js
+ end
+ end
+ end
+
+ def destroy
+ @project = Link.find(params[:id]).project
+ @collaborator = Link.find(params[:id]).collaborator
+
+ @project.remove_collaborator!(@collaborator)
+ flash[:success] = "Collaborator Removed"
+ respond_to do |format|
+ format.html {redirect_to @project}
+ format.js
+ end
+
+ end
+end
View
4 app/controllers/posts_controller.rb
@@ -2,11 +2,10 @@ class PostsController < ApplicationController
def create
@project = Project.find(params[:post][:project_id])
- @collaborator = Collaborator.find_by_email(params[:post][:collaborator])
+ @collaborator = @project.collaborators.find_by_email(params[:post][:collaborator])
if !@collaborator
- #redirect_to @project
else
@post = @project.posts.build(collaborator_id: @collaborator.id, content: params[:post][:content])
if @post.save
@@ -16,7 +15,6 @@ def create
format.js
end
end
- #redirect_to @project
end
end
end
View
1  app/controllers/projects_controller.rb
@@ -19,6 +19,7 @@ def show
@project = Project.find(params[:id])
@post = @project.posts.build
@posts = @project.post_list
+ @collaborators = @project.collaborators
end
def index
View
2  app/helpers/links_helper.rb
@@ -0,0 +1,2 @@
+module LinksHelper
+end
View
2  app/models/collaborator.rb
@@ -1,6 +1,8 @@
class Collaborator < ActiveRecord::Base
attr_accessible :email, :name
has_many :posts
+ has_many :links, dependent: :destroy
+ has_many :projects, through: :links
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
View
9 app/models/link.rb
@@ -0,0 +1,9 @@
+class Link < ActiveRecord::Base
+ attr_accessible :collaborator_id
+
+ belongs_to :collaborator
+ belongs_to :project
+
+ validates :collaborator_id, presence: true
+ validates :project_id, presence: true
+end
View
10 app/models/project.rb
@@ -1,6 +1,8 @@
class Project < ActiveRecord::Base
attr_accessible :description, :end, :name, :start
has_many :posts, dependent: :destroy
+ has_many :links, dependent: :destroy
+ has_many :collaborators, through: :links
VALID_DATE_REGEX = /\d\d-\d\d-\d\d\d\d/
@@ -13,4 +15,12 @@ def post_list
Post.where("project_id = ?", id)
end
+ def add_collaborator!(collaborator)
+ links.create!(collaborator_id: collaborator.id)
+ end
+
+ def remove_collaborator!(collaborator)
+ links.find_by_collaborator_id(collaborator.id).destroy
+ end
+
end
View
3  app/views/collaborators/_project.html.erb
@@ -0,0 +1,3 @@
+<li>
+ <%= link_to project.name, project %>
+</li>
View
12 app/views/collaborators/show.html.erb
@@ -3,7 +3,17 @@
<h1>Name: <%= @collaborator.name %></h1>
<h3>Email: <%= @collaborator.email %></h3>
<div>
- <%= link_to "Update Collaborator Profile", edit_collaborator_path(@collaborator), class: "btn-primary" %>
+ <%= link_to "Update Collaborator Profile", edit_collaborator_path(@collaborator), class: "btn btn-primary" %>
+</div>
+<div>
+ <p>I am working on the below projects.</p>
+ <div>
+ <% if @projects.any? %>
+ <ul>
+ <%= render @projects %>
+ </ul>
+ <% end %>
+ </div>
</div>
<div>
<p>Posts for this colaborator</p>
View
2  app/views/links/create.js.erb
@@ -0,0 +1,2 @@
+<% @collaborators = @project.collaborators %>
+$("#coll_list").html("<ul><%= escape_javascript(render @collaborators) %></ul>")
View
2  app/views/links/destroy.js.erb
@@ -0,0 +1,2 @@
+<% @collaborators = @project.collaborators %>
+$("#coll_list").html("<ul><%= escape_javascript(render @collaborators) %></ul>")
View
8 app/views/projects/_coll_add_form.html.erb
@@ -0,0 +1,8 @@
+ <%= form_for(@project.links.build, remote: true) do |f| %>
+ <%= f.hidden_field :project_id %>
+
+ <%= f.label :collaborator, "Collaborator Email" %>
+ <%= f.text_field :collaborator %>
+
+ <%= f.submit "Add Collaborator to Project", class: "btn btn-primary" %>
+ <% end %>
View
4 app/views/projects/_collaborator.html.erb
@@ -0,0 +1,4 @@
+<li>
+ <%= link_to collaborator.name, collaborator %>
+ <%= link_to "Remove Collaborator", link_path(@project.links.find_by_collaborator_id(collaborator.id)), method: :delete, remote: true %>
+</li>
View
2  app/views/projects/_post_form.html.erb
@@ -4,7 +4,7 @@
<%= f.label :content, "Content" %>
<%= f.text_area :content %>
- <%= f.label :collaborator, "Collaborator Name" %>
+ <%= f.label :collaborator, "Collaborator Email" %>
<%= f.text_field :collaborator %>
<%= f.submit "Create Post", class: "btn btn-primary" %>
View
19 app/views/projects/show.html.erb
@@ -5,7 +5,7 @@
<p>Start Date: <%= @project.start %></p>
<p>End Date: <%= @project.end %></p>
<div>
- <%= link_to "Update Project Profile", edit_project_path(@project), class: "btn-primary" %>
+ <%= link_to "Update Project Profile", edit_project_path(@project), class: "btn btn-primary" %>
</div>
<div>
<p>Posts for this project</p>
@@ -18,5 +18,20 @@
</div>
</div>
<div>
+ <p>Collaborators on this project</p>
+ <div id="coll_list">
+ <% if @collaborators.any? %>
+ <ul>
+ <%= render @collaborators %>
+ </ul>
+ <% end %>
+ </div>
+</div>
+<div>
+ <p>Use the form below to add collaborators to this project</p>
+ <%= render 'coll_add_form' %>
+</div>
+<div>
+ <p>Use the form below to make posts and discuss this project</p>
<%= render 'post_form' %>
-</div
+</div>
View
5 config/routes.rb
@@ -1,9 +1,14 @@
KashWebsite::Application.routes.draw do
+ get "links/create"
+
+ get "links/destroy"
+
root to: 'staticpages#home'
resources :projects, only: [:new, :create, :edit, :update, :index, :show]
resources :collaborators, only: [:create, :edit, :update, :index, :show]
resources :posts, only: :create
+ resources :links, only: [:create, :destroy]
# The priority is based upon order of creation:
# first created -> highest priority.
View
13 db/migrate/20120426025348_create_links.rb
@@ -0,0 +1,13 @@
+class CreateLinks < ActiveRecord::Migration
+ def change
+ create_table :links do |t|
+ t.integer :collaborator_id
+ t.integer :project_id
+
+ t.timestamps
+ end
+ add_index :links, :collaborator_id
+ add_index :links, :project_id
+ add_index :links, [:collaborator_id, :project_id], unique: true
+ end
+end
View
13 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120417024152) do
+ActiveRecord::Schema.define(:version => 20120426025348) do
create_table "collaborators", :force => true do |t|
t.string "name"
@@ -23,6 +23,17 @@
add_index "collaborators", ["email"], :name => "index_collaborators_on_email", :unique => true
add_index "collaborators", ["name"], :name => "index_collaborators_on_name"
+ create_table "links", :force => true do |t|
+ t.integer "collaborator_id"
+ t.integer "project_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "links", ["collaborator_id", "project_id"], :name => "index_links_on_collaborator_id_and_project_id", :unique => true
+ add_index "links", ["collaborator_id"], :name => "index_links_on_collaborator_id"
+ add_index "links", ["project_id"], :name => "index_links_on_project_id"
+
create_table "posts", :force => true do |t|
t.integer "collaborator_id"
t.integer "project_id"
Please sign in to comment.
Something went wrong with that request. Please try again.