Class: IshManager::NewsitemsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#home

Instance Method Details

#createObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/ish_manager/newsitems_controller.rb', line 21

def create
  @newsitem = Newsitem.new params[:newsitem].permit!
  @resource = City.find params[:city_id] if params[:city_id]
  @resource = City.find params[:newsitem][:city_id] if !params[:newsitem][:city_id].blank?
  @resource = Site.find params[:site_id] if params[:site_id]
  @resource = Site.find params[:newsitem][:site_id] if !params[:newsitem][:site_id].blank?
  @resource.newsitems << @newsitem

  authorize! :create_newsitem, @resource

  if params[:photo]
    photo = Photo.create :photo => params[:photo], :user_profile => current_user.profile
    @newsitem.photo = photo
  end

  flag = @newsitem.save && @resource.save
  if flag
    @resource.touch
  else
    error = 'No Luck. ' + @newsitem.errors.messages.to_s  + " :: " + photo.errors.messages.to_s
  end
  url = params[:city_id] ? edit_city_path( @resource.id ) : edit_site_path( @resource.id )
  
  if flag
    flash[:notice] = 'Success'
    redirect_to url
  else
    flash[:alert] = error
    @sites = Site.list
    @cities = City.list

    render :action => :new
  end
end

#destroyObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/ish_manager/newsitems_controller.rb', line 56

def destroy
  authorize! :destroy, Newsitem
  if params[:city_id]
    flag = City.find( params[:city_id] ).newsitems.find( params[:id] ).destroy
    url = edit_city_path( params[:city_id] )
  end
  if params[:site_id]
    site = Site.find( params[:site_id] )
    flag = site.newsitems.find( params[:id] ).destroy
    url = edit_site_path( params[:site_id] )
  end

  flash[:notice] = "Success? #{flag}"
  redirect_to url
end

#editObject



101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/ish_manager/newsitems_controller.rb', line 101

def edit
  if params[:site_id]
    @site = Site.find params[:site_id]
    @newsitem = @site.newsitems.find( params[:id] )
  end
  if params[:city_id]
    @city = City.find params[:city_id]
    @newsitem = @city.newsitems.find( params[:id] )
  end
  authorize! :edit, @newsitem
end

#indexObject



113
114
115
116
117
118
# File 'app/controllers/ish_manager/newsitems_controller.rb', line 113

def index
  @resource = Site.find( params[:site_id] ) if params[:site_id]
  @resource = City.find( params[:site_id] ) if params[:city_id]
  authorize! :newsitems_index, @resource
  @newsitems = @resource.newsitems
end

#newObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/ish_manager/newsitems_controller.rb', line 6

def new
  @newsitem = Newsitem.new
  @sites = Site.list
  @cities = City.list
  if params[:city_id]
    @city = City.find params[:city_id]
    @newsitem.city = @city
  end
  if params[:site_id]
    @site = Site.find params[:site_id]
    @newsitem.site = @site
  end
  authorize! :new, @newsitem
end

#updateObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/ish_manager/newsitems_controller.rb', line 72

def update
  if params[:site_id]
    @site = Site.find params[:site_id]
    @site.touch
    @newsitem = @site.newsitems.find params[:id]
    url = edit_site_path( @site )
  end
  if params[:city_id]
    @city = City.find params[:city_id]
    @newsitem = @city.newsitems.find params[:id]
    url = edit_city_path( @city )
  end
  if params[:photo]
    photo = Photo.new :photo => params[:photo]
    photo. = current_user.profile
    @newsitem.photo = photo
  end
  authorize! :update, @newsitem
  flag = @newsitem.update_attributes params[:newsitem].permit!
  
  if flag
    flash[:notice] = 'Success'
  else
    flash[:error] = "No Luck: #{@newsitem.errors.messages}"
  end

  redirect_to url
end