Woody88 / ESTC-CEPEv2

0 stars 0 forks source link

Shift Model Structure #1

Open Woody88 opened 9 years ago

Woody88 commented 9 years ago

Having problem picturing the structure of my shift model. I want the user to be able to trade or swap shift through the PostedShift controller or send a direct message that request a trade. But since I dont have messages system integrated yet, I will just stick with trade that can be done through PostedShift controller. So I'm thinking of something of this sort(see below) but I heard alot about having slim controller and fat model. Is this logic correct or good practice?

 @seller = User.find(params[:tid])  #finds seller's 
  @buyer = User.find(current_user.id) #finds buyer
  @shift = @seller.shifts.find(params[:sid]) #finds seller's 
  @bshift = @buyer.shifts.find_by(:date => @shift.date)  #finds buyer's shift on date that wants to pick up
  if @bshift.nil? or !@bshift.overlaps?(@shift) #if empty or shift does not overlap with buyers original shift then proceed for swap.
    @nshift = @buyer.shifts.new
    @nshift = @shift.dup
    @nshift.owner_id = @buyer.id
    @nshift.shift_posted = "Not Posted"
    @nshift.save
    @shift.posted_shift.status = "Traded"
    @shift.posted_shift.save
    @shift.save
    @shift.destroy
coalwater commented 9 years ago

where does this code exist? in which controller or which model ?

Woody88 commented 9 years ago

In the PostedShift controller so when some clicks on the link pick_up thats when the trade it triggered.

but i modify it to this(see below) class PostedShiftsController < ApplicationController

   def pick_up
     @buyer = Profile.find_by(:user_id => current_user.id)
     @shift = Shift.find_by(:shift_id => params[:sid], :current_owner => params[:uid] )
     @bshift = @buyer.shifts.find_by(:date => @shift.date)
     if @bshift.nil? or !@bshift.overlaps?(@shift) 
       @trade = Shift.confirm_trade(@shift)
       @trade.current_owner = current_user.id
       @trade.shift_posted = "Not Posted"
       if @trade.save
          @shift.destroy
       end
    respond_with(@trade, :location => shifts_url)
   end
  end
end
Woody88 commented 9 years ago

In the Shift model I added this

 class Shift < ActiveRecord::Base

 def self.confirm_trade(seller)
    attrs = ["position", "date", "start_time", "finish_time", "original_owner"]
    self.new(seller.attributes_and_values(attrs))
end

def attributes_and_values(array)
    result = Hash.new
    array.each do |name|
        result[name] = self.attributes[name]
    end
    result
end
end
Woody88 commented 9 years ago

I'm not sure if its the correct way to do things, So its supposed to copy the record of the seller into the buyer and delete the record of the seller.

coalwater commented 9 years ago

why not just change the shift's user_id ?

Woody88 commented 9 years ago

Mohammed omg... I think that I was just thinking just way too complicated.. anyhow I have made the change by using user_id. And of course it works. But there is a problem with shift controller. This code runs perfectly until this part '@shift.shift_posted = "Posted" ' for some reason it does not make that change..

 def create
   @user = Profile.find(current_user.id)
   @shift = @user.shifts.find_by(params[:id])
   @posted_shift = PostedShift.new(posted_shift_params)
   @posted_shift.seller_id = current_user.id
   @posted_shift.st_id = @shift.id
   @shift.shift_posted = "Posted"                      #right here its as if it completely ignores it.
   if @posted_shift.save && @shift.save
     flash[:notice] = "Shift Posted For Trade"
   end
  respond_with(@posted_shift, :location => @posted_shifts_path)

end