I have three models:
class User < ActiveRecord::Base
has_many :administrations
has_many :calendars, through: :administrations
end
class Calendar < ActiveRecord::Base
has_many :administrations
has_many :users, through: :administrations
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
Remark: I am using Devise for authentication on the User
model.
The join Administration
model has the following attributes:
id
user_id
calendar_id
role
Here are my current routes:
devise_for :users, :path => 'account'
resources :users do
resources :calendars
end
And here is my calendars_controller
:
class CalendarsController < ApplicationController
before_action :set_calendar, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /calendars
# GET /calendars.json
def index
@user = current_user
@calendars = Calendar.all
end
# GET /calendars/1
# GET /calendars/1.json
def show
@user = current_user
@calendar = Calendar.find(params[:id])
end
# GET /calendars/new
def new
@user = current_user
@calendar = @user.calendars.new
end
# GET /calendars/1/edit
def edit
@user = current_user
end
# POST /calendars
# POST /calendars.json
def create
@user = current_user
@calendar = @user.calendars.new(calendar_params)
respond_to do |format|
if @calendar.save
format.html { redirect_to user_calendar_path(@user,@calendar), notice: 'Calendar was successfully created.' }
format.json { render :show, status: :created, location: @calendar }
else
format.html { render :new }
format.json { render json: @calendar.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /calendars/1
# PATCH/PUT /calendars/1.json
def update
@user = current_user
@calendar = Calendar.find(params[:id])
respond_to do |format|
if @calendar.update(calendar_params)
format.html { redirect_to user_calendar_path(@user,@calendar), notice: 'Calendar was successfully updated.' }
format.json { render :show, status: :ok, location: @calendar }
else
format.html { render :edit }
format.json { render json: @calendar.errors, status: :unprocessable_entity }
end
end
end
# DELETE /calendars/1
# DELETE /calendars/1.json
def destroy
@user = current_user
@calendar.destroy
respond_to do |format|
format.html { redirect_to user_calendars_url, notice: 'Calendar was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_calendar
@calendar = Calendar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def calendar_params
params.require(:calendar).permit(:name)
end
end
My basic CRUD actions are working: once a user
is logged in, he can create
, show
, edit
and destroy
a calendar
that belongs to him.
What I am trying to figure out now, is how to define a default role
— Owner — in the Administration
join table when a user
creates a new calendar
.
I was considering adding the following line of code to Calendars#Create
:
current_user.add_calendar_and_role(@calendar.id, 'Owner')
but I am not sure this is a good practice.
Instead, shouldn't I define a make_owner
method in my administration
model and use it with a callback in the Calendars#Create
action?
Should I also implement accepts_nested_attributes_for
in my calendar
model?
Or is there an even better solution that I am forgetting about?
In general - move code to the model when you can. Keep your controllers simple.
I don't think you need accepts nested attributes. I tend to use that when my models are truly child objects, not just joined.
Your datamodel looks good - I think whatever you choose, you'll be able to improve it later or handle more special cases. That said, I do this sort of thing
class Administration < ActiveRecord::Base
before_validation :set_default_role
def set_default_role
existing_owner = where(:calendar_id => calendar_id, :role => :owner).exists?
if existing_owner?
self.role ||= :standard
else
self.role ||= :owner
end
end
end
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments