Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def index
- @user = current_user
- @projects = @user.projects
- end
- def create
- @project = Project.new(project_params)
- @project.user_id = current_user.id
- if @project.save
- flash[:success] = "successfully created project"
- redirect_to projects_path
- else
- render 'new'
- end
- end
- class Invite < ApplicationRecord
- belongs_to :project
- belongs_to :sender, :class_name => 'User'
- belongs_to :recipient, :class_name => 'User'
- before_create :generate_token
- before_save :check_user_existence
- def generate_token
- self.token = Digest::SHA1.hexdigest([self.project_id, Time.now, rand].join)
- end
- def check_user_existence
- recipient = User.find_by_email(email)
- if recipient
- self.recipient_id = recipient.id
- end
- end
- end
- class User < ApplicationRecord
- has_many :memberships
- has_many :projects, through: :memberships
- has_many :invitations, :class_name => 'Invite', :foreign_key => 'recipient_id'
- has_many :sent_invites, :class_name => 'Invite', :foreign_key => 'sender_id'
- # Include default devise modules. Others available are:
- # :confirmable, :lockable, :timeoutable and :omniauthable
- devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable
- end
- class Project < ApplicationRecord
- has_many :memberships
- has_many :users, through: :memberships
- has_many :invites
- end
- class Membership < ApplicationRecord
- belongs_to :user
- belongs_to :project
- end
- <div class="container">
- <h3> All your projects </h3>
- <% @projects.each do |project| %>
- <div class="project-card">
- <div class="card-title">
- <%= link_to project.title, project_path(project) %>
- </div>
- </div>
- <% end %>
- </div>
- ActiveRecord::Schema.define(version: 20161211133001) do
- create_table "invites", force: :cascade do |t|
- t.string "email"
- t.integer "project_id"
- t.integer "sender_id"
- t.integer "recipient_id"
- t.string "token"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- end
- create_table "memberships", force: :cascade do |t|
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.integer "user_id"
- t.integer "project_id"
- t.index ["project_id"], name: "index_memberships_on_project_id"
- t.index ["user_id"], name: "index_memberships_on_user_id"
- end
- create_table "projects", force: :cascade do |t|
- t.string "title"
- t.integer "nb_team"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.integer "user_id"
- end
- create_table "users", force: :cascade do |t|
- t.string "email", default: "", null: false
- t.string "encrypted_password", default: "", null: false
- t.string "reset_password_token"
- t.datetime "reset_password_sent_at"
- t.datetime "remember_created_at"
- t.integer "sign_in_count", default: 0, null: false
- t.datetime "current_sign_in_at"
- t.datetime "last_sign_in_at"
- t.string "current_sign_in_ip"
- t.string "last_sign_in_ip"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.index ["email"], name: "index_users_on_email", unique: true
- t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement