From badd1d1c18c18ef89203db2c3c2a42a72471ef22 Mon Sep 17 00:00:00 2001 From: Tamara Rivera Date: Thu, 12 Dec 2013 12:24:30 -0300 Subject: [PATCH] Add the relationship between users and projects and add a fix to the footer --- app/assets/stylesheets/custom.css.scss | 41 +++++++++---------- app/controllers/static_pages_controller.rb | 1 + app/models/project.rb | 5 +++ app/models/user.rb | 2 + app/models/user_project.rb | 7 ++++ app/views/layouts/_footer.html.erb | 20 +++++---- app/views/layouts/application.html.erb | 16 ++++---- app/views/shared/_project.html.erb | 8 ++++ app/views/static_pages/home.html.erb | 13 +++++- .../20131210150951_create_user_projects.rb | 13 ++++++ db/schema.rb | 13 +++++- lib/tasks/sample_projects.rake | 24 +++++++++++ lib/tasks/sample_users.rake | 19 --------- test/fixtures/user_projects.yml | 9 ++++ test/models/user_project_test.rb | 7 ++++ 15 files changed, 138 insertions(+), 60 deletions(-) create mode 100644 app/models/user_project.rb create mode 100644 app/views/shared/_project.html.erb create mode 100644 db/migrate/20131210150951_create_user_projects.rb delete mode 100644 lib/tasks/sample_users.rake create mode 100644 test/fixtures/user_projects.yml create mode 100644 test/models/user_project_test.rb diff --git a/app/assets/stylesheets/custom.css.scss b/app/assets/stylesheets/custom.css.scss index b92c077..fa650f1 100644 --- a/app/assets/stylesheets/custom.css.scss +++ b/app/assets/stylesheets/custom.css.scss @@ -60,29 +60,26 @@ p { } /* footer */ +html, +body { + height: 100%; + /* The html and body elements cannot have any padding or margin. */ +} -footer { - margin-top: 45px; - padding-top: 5px; - border-top: 1px solid #eaeaea; - color: $gray-light; - a { - color: #555; - } - small { - float: left; - } - ul { - float: right; - list-style: none; - li { - float: left; - margin-left: 10px; - } - } - &:hover { - color: #222; - } +/* Wrapper for page content to push down footer */ +#wrap { + min-height: 100%; + height: auto; + /* Negative indent footer by its height */ + margin: 0 auto -60px; + /* Pad bottom by footer height */ + padding: 0 0 60px; +} + +/* Set the fixed height of the footer here */ +#footer { + height: 60px; + background-color: #f5f5f5; } /* sidebar */ diff --git a/app/controllers/static_pages_controller.rb b/app/controllers/static_pages_controller.rb index 19f79a9..e2e165d 100644 --- a/app/controllers/static_pages_controller.rb +++ b/app/controllers/static_pages_controller.rb @@ -1,5 +1,6 @@ class StaticPagesController < ApplicationController def home + @projects = current_user.projects end def help diff --git a/app/models/project.rb b/app/models/project.rb index fa477d6..393013f 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,3 +1,8 @@ class Project < ActiveRecord::Base + has_many :user_projects, dependent: :destroy + has_many :users, through: :user_projects + def add_user!(user) + user_projects.create!(user_id: user.id) + end end diff --git a/app/models/user.rb b/app/models/user.rb index c15da36..90a4d03 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,6 @@ class User < ActiveRecord::Base + has_many :user_projects, dependent: :destroy + has_many :projects, through: :user_projects has_secure_password before_save { |user| user.email = email.downcase } diff --git a/app/models/user_project.rb b/app/models/user_project.rb new file mode 100644 index 0000000..1f6aac3 --- /dev/null +++ b/app/models/user_project.rb @@ -0,0 +1,7 @@ +class UserProject < ActiveRecord::Base + belongs_to :user + belongs_to :project + + validates :user_id, presence: true + validates :project_id, presence: true +end diff --git a/app/views/layouts/_footer.html.erb b/app/views/layouts/_footer.html.erb index b6a44b8..be411c5 100644 --- a/app/views/layouts/_footer.html.erb +++ b/app/views/layouts/_footer.html.erb @@ -1,9 +1,11 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 367053b..c43dd4e 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,13 +10,15 @@ - <%= render 'layouts/header' %> -
- <% flash.each do |key, value| %> -
<%= value %>
- <% end %> - <%= yield %> - <%= render 'layouts/footer' %> +
+ <%= render 'layouts/header' %> +
+ <% flash.each do |key, value| %> +
<%= value %>
+ <% end %> + <%= yield %> +
+ <%= render 'layouts/footer' %> \ No newline at end of file diff --git a/app/views/shared/_project.html.erb b/app/views/shared/_project.html.erb new file mode 100644 index 0000000..de6d2a8 --- /dev/null +++ b/app/views/shared/_project.html.erb @@ -0,0 +1,8 @@ +
+ <%= link_to (project) do %> + + <%end %> +
\ No newline at end of file diff --git a/app/views/static_pages/home.html.erb b/app/views/static_pages/home.html.erb index 376f6c4..2613428 100644 --- a/app/views/static_pages/home.html.erb +++ b/app/views/static_pages/home.html.erb @@ -1,5 +1,14 @@
+ <%if signed_in? %> +
+ <%if @projects.any?%> +
+ <%= render partial: 'shared/project', collection: @projects %> +
+ <%end%> +
+ <%else %>

Bienvenido a Pinkunozo

- - <%= link_to "Identificarse", signin_path, class: "btn btn-large btn-primary" %> + <%= link_to "Identificarse", signin_path, class: "btn btn-lg btn-primary" %> + <%end %>
\ No newline at end of file diff --git a/db/migrate/20131210150951_create_user_projects.rb b/db/migrate/20131210150951_create_user_projects.rb new file mode 100644 index 0000000..3c8b4ca --- /dev/null +++ b/db/migrate/20131210150951_create_user_projects.rb @@ -0,0 +1,13 @@ +class CreateUserProjects < ActiveRecord::Migration + def change + create_table :user_projects do |t| + t.integer :user_id + t.integer :project_id + + t.timestamps + end + add_index :user_projects, :user_id + add_index :user_projects, :project_id + add_index :user_projects, [:user_id, :project_id], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 461fe0a..485081e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20131205235724) do +ActiveRecord::Schema.define(version: 20131210150951) do create_table "projects", force: true do |t| t.string "name" @@ -20,6 +20,17 @@ t.datetime "updated_at" end + create_table "user_projects", force: true do |t| + t.integer "user_id" + t.integer "project_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "user_projects", ["project_id"], name: "index_user_projects_on_project_id" + add_index "user_projects", ["user_id", "project_id"], name: "index_user_projects_on_user_id_and_project_id", unique: true + add_index "user_projects", ["user_id"], name: "index_user_projects_on_user_id" + create_table "users", force: true do |t| t.string "name" t.string "email" diff --git a/lib/tasks/sample_projects.rake b/lib/tasks/sample_projects.rake index 277c920..47c622d 100644 --- a/lib/tasks/sample_projects.rake +++ b/lib/tasks/sample_projects.rake @@ -1,7 +1,31 @@ namespace :db do desc "Fill database with sample projects" task populate: :environment do + make_users + make_projects + end + + def make_users + User.create!(name: "Tamara Rivera", + email: "tamita.rivera@gmail.com", + password: "memoria", + password_confirmation: "memoria", + admin: true) + 99.times do |n| + name = Faker::Name.name + email = "example-#{n+1}@railstutorial.org" + password = "password" + User.create!(name: name, + email: email, + password: password, + password_confirmation: password) + end + end + + def make_projects Project.create!(name: "Proyecto Fondef", description: "Este es un proyecto fabuloso") + project = Project.first + project.add_user!(User.first) end end \ No newline at end of file diff --git a/lib/tasks/sample_users.rake b/lib/tasks/sample_users.rake deleted file mode 100644 index b5dfa51..0000000 --- a/lib/tasks/sample_users.rake +++ /dev/null @@ -1,19 +0,0 @@ -namespace :db do - desc "Fill database with sample users" - task populate: :environment do - User.create!(name: "Tamara Rivera", - email: "tamita.rivera@gmail.com", - password: "memoria", - password_confirmation: "memoria", - admin: true) - 99.times do |n| - name = Faker::Name.name - email = "example-#{n+1}@railstutorial.org" - password = "password" - User.create!(name: name, - email: email, - password: password, - password_confirmation: password) - end - end -end \ No newline at end of file diff --git a/test/fixtures/user_projects.yml b/test/fixtures/user_projects.yml new file mode 100644 index 0000000..174abfc --- /dev/null +++ b/test/fixtures/user_projects.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + user_id: 1 + project_id: 1 + +two: + user_id: 1 + project_id: 1 diff --git a/test/models/user_project_test.rb b/test/models/user_project_test.rb new file mode 100644 index 0000000..30f8232 --- /dev/null +++ b/test/models/user_project_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserProjectTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end