<file_sep>class Picture < ApplicationRecord
#picture belongs to event and user relation 
    belongs_to :imageable, polymorphic: true
	mount_uploader :picture, PictureUploader
#picture resize validation
	validate  :picture_size
private
# Validates the size of an uploaded picture.
	def picture_size
	    if picture.size > 5.megabytes
	        errors.add(:picture, "should be less than 5MB")
	    end
	end
end<file_sep>class Event < ApplicationRecord
belongs_to :creator, foreign_key: 'user_id', class_name: 'User'
#realtion of event attending
	has_many :event_attendings, :foreign_key => :attended_event_id
	has_many :attendees, :through => :event_attendings, :source => :event_attendee
#realtion of hasmany picture
	has_many :pictures, as: :imageable
	accepts_nested_attributes_for :pictures
	mount_uploader :picture, PictureUploader
#validation		
	default_scope -> { order(created_at: :desc) }
	validates :user_id, presence: true
	validates :title, presence: true,
	                    length: { maximum: 50 }
	validates :description, length: { maximum: 1000 }
	validates :location, presence: true, 
	                    length: { maximum: 50 }
#where query for find date and time wise event record
	scope :upcoming, -> { where("date >= ?", Time.now).order('date ASC') }
	scope :past, -> { where("date < ?", Time.now).order('date DESC') }
end
<file_sep>class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update ,:destroy] 
#show all user 
def index
		@user = User.find_by(params[:id])
        @users = User.all
		if params[:user_id].present?
        	@events = Event.where(user_id: params[:user_id])
        else
        	@events = Event.where(user_id: current_user.id)
        end
	end
#update user 
	def update
		if @user.update_attributes(user_params)
	    	flash[:success] = "Profile updated"
	    	redirect_to @user
	    else
	      	render :edit
	    end
	end
# Use callbacks to share common setup or constraints between actions.
private
	def set_user
	     @user = User.find(params[:id])
	end
	def user_params
	    params.require(:user).permit(:name, :email,:phone_number,:gender,:date_of_birth,:password,
	                                     :password_confirmation,:current_password,:role,pictures_attributes: :picture)
	end
end






<file_sep> class RegistrationsController < Devise::RegistrationsController
 	def new
 	    build_resource
 		self.resource.pictures.build
 	end
    
    private
    def sign_up_params
   		params.require(:user).permit(:email,:name,:phone_number,:gender,:date_of_birth,:password, :password_confirmation,:current_password,:role,pictures_attributes: [:picture])
    end
    def account_update_params
     	params.require(:user).permit(:email,:name,:phone_number,:gender,:date_of_birth,:password, :password_confirmation,:current_password,:role,pictures_attributes: [:picture])
    end
end<file_sep>class EventAttendee < ApplicationRecord
	belongs_to :attendee, :class_name => "User"
	belongs_to :event,    :class_name => "Event"

	validates :attendee_id, presence: true
	validates :event_id, presence: true

end
<file_sep>class EventPolicy < ApplicationPolicy	
	def new
		is_admin? 
	end

	def show?
		is_admin? || is_user?
	end

	def create?
		is_admin? 
	end

	def update?
		is_admin? 
	end

	def edit?
		is_admin?
	end

	def destroy?
		is_admin? 
	end

	def index?
		is_user?
	end
end








<file_sep>class Role < ApplicationRecord
has_and_belongs_to_many :users, :join_table => :users_roles
#add role for user for authorization 
belongs_to :resource,:polymorphic => true,:optional => true
#validation for rolify 
validates :resource_type,:inclusion => { :in => Rolify.resource_types },:allow_nil => true
scopify
end
<file_sep>class EventController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy,:index]
#new event render
  def new
    @event = Event.new 
    @event.pictures.build
  end

#attndeing method for user
  def attend
    @event = Event.find_by(id: params[:id])
    @event.attendees << current_user
    redirect_to current_user
  end

#show all event 
  def index
    @upcoming_events= Event.upcoming.all
    @past_events = Event.past.all
    if params[:user_id].present?
      @events = Event.where(user_id: params[:user_id])
    else
      @events = Event.where(user_id: current_user.id)
    end 
    authorize @events
  end  

#craete new event and save
  def create
    @event = current_user.events.build(event_params)
    authorize @event
    if @event.save!
      @event.attendees << current_user  
      flash[:success] = "Event created successfully...!!!!!!!!!!"
      redirect_to current_user
    else
      flash[:notice] = "Event is not crrated plase fill the vaule...!!!!!!!!!!"
      redirect_to root_url
    end
  end

#destory event
  def destroy
    @event = Event.find_by(params[:event_id])
    if @event.destroy
      authorize @event
      flash[:success] = "Event Deleted"
      redirect_back(fallback_location: root_url)
    else
      flash[:notice] = "event is not deteled plzz try again!!!!"
      redirect_to root_path
    end
  end

#update event
  def update
    @event = Event.find(params[:id])
    authorize @event
    if@event.update(event_params)
        redirect_back(fallback_location: root_url)
    else
      render :edit
    end
  end
  
#private method for event params
  private
  def event_params
    params.require(:event).permit(:date, :location, :title, :description, :user_id , pictures_attributes: :picture)
  end

end<file_sep>Rails.application.routes.draw do
 
  
  get 'picture/new'
  devise_for :users, :controllers => { registrations: 'registrations' }
  root 'static_pages#home'
  get '/about', to:'static_pages#about'
  get '/contact', to:'static_pages#contact'
  get '/news',  to:'static_pages#news'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  resources :users, only: [:index, :show, :edit, :update ,:destroy] 



  get 'event/new' 

  post 'event/create' 

  get 'event/index'
  get 'event/show'
  get 'event/update'
  get 'event/destroy'

  resources :event do
     post 'attend' ,on: :member
   end

 
  #resources :event_attendees, only: [:create, :destroy]
end
<file_sep>class User < ApplicationRecord
#rolify add role
rolify
after_create :assign_default_role 
#relation for attend event 
has_many :events, foreign_key: 'user_id', class_name: 'Event'  ,dependent: :destroy
has_many :event_attendings, :foreign_key => :event_attendee_id
has_many :attended_events, :through => :event_attendings
#relation for picture uploder 
has_many :pictures, as: :imageable
accepts_nested_attributes_for :pictures
#Include default devise modules.
devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
#validtion for user signin from
validates :name,  presence: true
#validates :phone_number, presence: true
#validates :gender, presence: { message: "must be selected" } 
#validates :dob, presence: { message: "(Date of Birth) must be entered" }   end
#when user singup then assign role
  def assign_default_role
    if self.roles.blank?
      self.add_role(:user)
    else
      self.add_role(:admin)
    end
  end

  def upcoming_events
		self.attended_events.upcoming
	end
	def past_events
		self.attended_events.past
	end
end

<file_sep>module PictureHelper
	
end
<file_sep>module EventHelper

		
end
<file_sep>class PictureController < ApplicationController
before_action :load_imagable

#picture craete for event and user profile picture
def create
	@picture =@imageable.pictures.new(picture_params)  
	if @picture.save
	    redirect_to [@imageable, :pictures], notice: 'Comment created'
	else
		render :new
	end
	end 

#render picture from
	def new
		@picture  = @imageable.pictures.new
	end

#show picture
	def show
		@picture = Picture.find(params[:id])
	    @imageable = User.find(params[:user_id])
	    @imageable = Event.find(params[:event_id])
	end
	
#private method for picture params and load images
	private
	def picture_params
	    params.require(:picture).permit(:picture)
	end
	def load_imagable
	    resource, id = request.path.split('/')[1,2]
	    @imagable = resource.singularize.classify.constantize.find(id)
	end
end


