Class: IshManager::GalleriesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/ish_manager/galleries_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#home

Instance Method Details

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/ish_manager/galleries_controller.rb', line 32

def create
  params[:gallery][:shared_profiles] ||= []
  params[:gallery][:shared_profiles].delete('')
  params[:gallery][:shared_profiles] = IshModels::UserProfile.find params[:gallery][:shared_profiles]
  @gallery = Gallery.new params[:gallery].permit!
  @gallery. = current_user.profile
  @gallery.username = current_user.profile.username
  authorize! :create, @gallery

  if @gallery.save
    ::IshManager::ApplicationMailer.shared_galleries( params[:gallery][:shared_profiles], @gallery ).deliver
    flash[:notice] = 'Success'
    redirect_to galleries_path
  else
    puts! @gallery.errors.messages
    flash[:alert] = 'No Luck. ' + @gallery.errors.inspect
    @cities_list = City.list
    @tags_list = Tag.list
    render :action => 'new'
  end
end

#destroyObject



94
95
96
97
98
99
100
101
# File 'app/controllers/ish_manager/galleries_controller.rb', line 94

def destroy
  @gallery = Gallery.unscoped.find params[:id]
  authorize! :destroy, @gallery
  @gallery.is_trash = true
  @gallery.save
  flash[:notice] = 'Logically deleted gallery.'
  redirect_to galleries_path
end

#editObject



54
55
56
57
# File 'app/controllers/ish_manager/galleries_controller.rb', line 54

def edit
  @gallery = Gallery.unscoped.find params[:id]
  authorize! :edit, @gallery
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/ish_manager/galleries_controller.rb', line 5

def index
  authorize! :index, Gallery
  @galleries = Gallery.unscoped.where( :is_trash => false, :user_profile => current_user.profile
    ).order_by( :created_at => :desc )
  if params[:q]
    @galleries = @galleries.where({ :name => /#{params[:q]}/i })
  end
  @galleries = @galleries.page( params[:galleries_page] ).per( 20 )

  render params[:render_type]
end

#j_showObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/ish_manager/galleries_controller.rb', line 103

def j_show
  @gallery = Gallery.unscoped.find( params[:id] )
  authorize! :show, @gallery
  respond_to do |format|
    format.json do
      jjj = {}
      jjj[:photos] = @gallery.photos.map do |ph|
        { :thumbnail_url => ph.photo.url( :thumb ),
        :delete_type => 'DELETE',
        :delete_url => photo_path(ph) }
      end
      render :json => jjj
    end
  end
end

#newObject



25
26
27
28
29
30
# File 'app/controllers/ish_manager/galleries_controller.rb', line 25

def new
  @gallery = Gallery.new
  authorize! :new, @gallery
  @cities_list = City.list
  @tags_list = Tag.list
end

#shared_with_meObject



17
18
19
20
21
22
23
# File 'app/controllers/ish_manager/galleries_controller.rb', line 17

def shared_with_me
  authorize! :index, Gallery
  @galleries = current_user.profile.shared_galleries.unscoped.where( :is_trash => false
    ).order_by( :created_at => :desc
    ).page( params[:shared_galleries_page] ).per( 10 )
  render params[:render_type]
end

#showObject



83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/ish_manager/galleries_controller.rb', line 83

def show
  begin
    @gallery = Gallery.unscoped.find_by :galleryname => params[:id]
  rescue
    @gallery = Gallery.unscoped.find params[:id]
  end
  authorize! :show, @gallery
  @photos = @gallery.photos.unscoped.where({ :is_trash => false })
  @deleted_photos = @gallery.photos.unscoped.where({ :is_trash => true })
end

#updateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/ish_manager/galleries_controller.rb', line 59

def update
  @gallery = Gallery.unscoped.find params[:id]
  old_shared_profile_ids = @gallery.shared_profiles.map(&:id)
  authorize! :update, @gallery

  params[:gallery][:shared_profiles].delete('')
  # params[:gallery][:shared_profiles] = IshModels::UserProfile.find( params[:gallery][:shared_profiles] ).to_a
  params[:gallery][:shared_profile_ids] = params[:gallery][:shared_profiles]
  params[:gallery].delete :shared_profiles

  # puts! params[:gallery][:shared_profiles], 'shared profiles'
  if @gallery.update_attributes( params[:gallery].permit! )
    new_shared_profiles = IshModels::UserProfile.find( params[:gallery][:shared_profile_ids]
      ).select { |p| !old_shared_profile_ids.include?( p.id ) }
    ::IshManager::ApplicationMailer.shared_galleries( new_shared_profiles, @gallery ).deliver
    flash[:notice] = 'Success.'
    redirect_to galleries_path
  else
    puts! @gallery.errors.messages, 'cannot save gallery'
    flash[:alert] = 'No Luck. ' + @gallery.errors.messages.to_s
    render :action => :edit
  end
end