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

Create synapse associations with nifty-generators #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ group :production do
gem 'rails_12factor'
end

group :development do
gem 'nifty-generators'
end

# Bootstrap
gem 'bootstrap-sass', '2.3.2.0'

Expand All @@ -58,3 +62,5 @@ gem 'faker', '1.1.2'

# Use debugger
# gem 'debugger', group: [:development, :test]

gem "mocha", group: :test
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ GEM
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
metaclass (0.0.2)
mime-types (1.25.1)
minitest (4.7.5)
mocha (1.0.0)
metaclass (~> 0.0.1)
multi_json (1.8.4)
nifty-generators (0.4.6)
pg (0.17.1)
polyglot (0.3.3)
rack (1.5.2)
Expand Down Expand Up @@ -128,6 +132,8 @@ DEPENDENCIES
faker (= 1.1.2)
jbuilder (~> 1.2)
jquery-rails
mocha
nifty-generators
pg
rails (= 4.0.2)
rails_12factor
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/synapses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class SynapsesController < ApplicationController
def create
@synapse = current_user.synapses.build(:other_user_id => params[:other_user_id])
if @synapse.save
redirect_to root_url, :notice => "Successfully added!"
else
flash[:error] = "Sorry, something went wrong. We're working on it."
redirect_to root_url
end
end

def destroy
@synapse = current_user.synapses.find(params[:id])
@synapse.destroy
redirect_to current_user, :notice => "Removed connection."
end
end
2 changes: 2 additions & 0 deletions app/helpers/synapses_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SynapsesHelper
end
4 changes: 4 additions & 0 deletions app/models/synapse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Synapse < ActiveRecord::Base
belongs_to :user
belongs_to :other_user, :class_name => "User"
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy

has_many :synapses
has_many :other_users, :through => :synapses
has_many :inverse_synapses, :class_name => "Synapse", :foreign_key => "other_user_id"
has_many :inverse_other_users, :through => :inverse_synapses, :source => :user

before_save { self.email = email.downcase }
before_create :create_remember_token

Expand Down
7 changes: 6 additions & 1 deletion app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@
<h1>All users</h1>

<ul class="users">
<%= render @users %>
<% for user in User.all %>
<li>
<strong><%= user.name %></strong>
(<%= link_to "add", synapses_path(:other_user_id => user), :method => :post %>)
</li>
<% end %>
</ul>
16 changes: 16 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
<section>
<h1><%= @user.name %></h1>
</section>
<section>
<h2>Connections</h2>
<ul>
<% for synapse in @user.synapses %>
<li>
<%= synapse.other_user.name %>
(<%= link_to "remove", synapse, method: :delete %>)
</li>
<% end %>
<% for user in @user.inverse_other_users %>
<li>
<%= user.name %>
</li>
<% end %>
</ul>
</section>
</aside>
<div class="span8">
<% if @user.posts.any? %>
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

resources :posts, only: [:create, :destroy]

resources :synapses

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20140127184714_create_synapses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateSynapses < ActiveRecord::Migration
def self.up
create_table :synapses do |t|
t.integer :user_id
t.integer :other_user_id
t.timestamps
end
end

def self.down
drop_table :synapses
end
end
20 changes: 19 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140122232133) do
ActiveRecord::Schema.define(version: 20140127184714) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -27,6 +27,24 @@

add_index "posts", ["user_id", "created_at"], name: "index_posts_on_user_id_and_created_at", using: :btree

create_table "relationships", force: true do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id", using: :btree
add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true, using: :btree
add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id", using: :btree

create_table "synapses", force: true do |t|
t.integer "user_id"
t.integer "other_user_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", force: true do |t|
t.string "email"
t.string "name"
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/synapses.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
one:
user_id:
other_user_id:

two:
user_id:
other_user_id:
22 changes: 22 additions & 0 deletions test/functional/synapses_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'test_helper'

class SynapsesControllerTest < ActionController::TestCase
def test_create_invalid
Synapse.any_instance.stubs(:valid?).returns(false)
post :create
assert_template 'new'
end

def test_create_valid
Synapse.any_instance.stubs(:valid?).returns(true)
post :create
assert_redirected_to root_url
end

def test_destroy
synapse = Synapse.first
delete :destroy, :id => synapse
assert_redirected_to root_url
assert !Synapse.exists?(synapse.id)
end
end
7 changes: 7 additions & 0 deletions test/unit/synapse_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class SynapseTest < ActiveSupport::TestCase
def test_should_be_valid
assert Synapse.new.valid?
end
end