Class: RailsAdserver::AdvertisementsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_adserver/advertisements_controller.rb

Instance Method Summary collapse

Instance Method Details

#ad_paramObject



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

def ad_param
  id = RailsAdserver::Advertisement.ad_with_parameter(params[:adspace_id],params[:id],request.remote_ip)
  if id == nil
    @advertisement = nil
  else
    @advertisement = RailsAdserver::Advertisement.find(id)
  end
  respond_to do |format|
    format.html {render :partial => 'advertisement', :layout => false}
  end
end

#ad_spaceObject



17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/rails_adserver/advertisements_controller.rb', line 17

def ad_space
  id = RailsAdserver::Advertisement.ad(params[:adspace_id],request.remote_ip)
  if id == nil
    @advertisement = nil
  else
    @advertisement = RailsAdserver::Advertisement.find(id)
  end
  respond_to do |format|
    format.html {render :partial => 'advertisement', :layout => false}
  end
end

#clickObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/rails_adserver/advertisements_controller.rb', line 29

def click
  client_ip = request.remote_ip
  ad = Advertisement.find(params[:id])
  ad.clicks.create(:ip_address => client_ip)
  ad.update_attribute(:click_count,ad.clicks.count)
  if ad.click_count >= ad.max_clicks
    unless ad.max_clicks == 0
      ad.update_attribute(:is_active,false)
    end
  end
  redirect_to ad.url
end

#createObject

POST /advertisements



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/rails_adserver/advertisements_controller.rb', line 78

def create
  @advertisement = Advertisement.new(params[:advertisement])
  geo = Geokit::Geocoders::MultiGeocoder.geocode(@advertisement.geolocation_location)
  @advertisement.update_attributes(:city_name => geo.city, :state_name => geo.state, :country_name => geo.country)
  
  if @advertisement.parameter_restriction_boolean == nil
    @advertisement.update_attributes(:parameter_restriction_boolean => false)
  end
  
  if @advertisement.geolocation_boolean == nil
    @advertisement.update_attributes(:geolocation_boolean => false)
  end
  
  respond_to do |format|
    if @advertisement.save
      format.html { redirect_to @advertisement, notice: 'Advertisement was successfully created.' }
      format.json { render json: @advertisement, status: :created, location: @advertisement }
    else
      format.html { render action: "new" }
      format.json { render json: @advertisement.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /advertisements/1



119
120
121
122
123
124
125
126
127
# File 'app/controllers/rails_adserver/advertisements_controller.rb', line 119

def destroy
  @advertisement = Advertisement.find(params[:id])
  @advertisement.destroy
  
  respond_to do |format|
    format.html { redirect_to advertisements_url }
    format.json { head :no_content }
  end
end

#editObject

GET /advertisements/1/edit



72
73
74
75
# File 'app/controllers/rails_adserver/advertisements_controller.rb', line 72

def edit
  @title = "Edit Advertisement"
  @advertisement = Advertisement.find(params[:id])
end

#indexObject

GET /advertisements



42
43
44
45
46
47
48
49
# File 'app/controllers/rails_adserver/advertisements_controller.rb', line 42

def index
  @advertisements = Advertisement.all
  @title = "Advertisements"
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @advertisements }
  end
end

#newObject

GET /advertisements/new



62
63
64
65
66
67
68
69
# File 'app/controllers/rails_adserver/advertisements_controller.rb', line 62

def new
  @advertisement = Advertisement.new
  @title = "New Advertisement"
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @advertisement }
  end
end

#showObject

GET /advertisements/1



52
53
54
55
56
57
58
59
# File 'app/controllers/rails_adserver/advertisements_controller.rb', line 52

def show
  @advertisement = Advertisement.find(params[:id])
  @title = @advertisement.title
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @advertisement }
  end
end

#updateObject

PUT /advertisements/1



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

def update
  @advertisement = Advertisement.find(params[:id])
  respond_to do |format|
    if @advertisement.update_attributes(params[:advertisement])
      geo = Geokit::Geocoders::MultiGeocoder.geocode(@advertisement.geolocation_location)
      @advertisement.update_attributes(:city_name => geo.city, :state_name => geo.state, :country_name => geo.country)
      format.html { redirect_to @advertisement, notice: 'Advertisement was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "edit" }
      format.json { render json: @advertisement.errors, status: :unprocessable_entity }
    end
  end
end