GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: A simple application written in merb to help you manage ad serving across multiple sites
Clone URL: git://github.com/kneath/greed.git
Simple campaigns view
kneath (author)
Fri Apr 25 18:33:55 -0700 2008
commit  44b0ec5dfbb0efc7d2e28ce5089441fe67e64f31
tree    7f64f6000c3c142801c43a0c0c71f1d1f16b8819
parent  401383b058e61dfe0bed3cf5f6b86dd66a45d088
...
1
2
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
@@ -1,6 +1,28 @@
0
 class Campaigns < Application
0
   
0
   def index
0
+ unless cookies[:selected_site_id].nil?
0
+ @site = Site.find(cookies[:selected_site_id])
0
+ else
0
+ @site = Site.find(:first)
0
+ end
0
+ redirect url(:action => "show", :site_id => @site.id)
0
+ rescue
0
+ redirect url(:controller => "sites", :action => "index")
0
+ end
0
+
0
+ def show
0
+ @sites = Site.find(:all)
0
+ @site = Site.find(params[:site_id])
0
+ @campaign = Campaign.new(params[:campaign])
0
+ cookies[:selected_site_id] = params[:site_id]
0
+ if request.method == :post or request.method == :put
0
+ @campaign = Campaign.new(params[:campaign].merge({:active => true}))
0
+ if @campaign.save
0
+ params[:campaign] = nil
0
+ end
0
+ @site.reload
0
+ end
0
     render
0
   end
0
   
...
1
2
3
4
 
 
 
5
6
7
...
1
2
3
 
4
5
6
7
8
9
0
@@ -1,6 +1,8 @@
0
 class Campaign < ActiveRecord::Base
0
   
0
   belongs_to :spot
0
- has_one :advertisement
0
+ has_one :advertisement, :dependent => :destroy
0
+
0
+ validates_presence_of :name, :starts_on, :ends_on, :spot_id
0
   
0
 end
0
\ No newline at end of file
...
7
8
9
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
12
13
14
...
7
8
9
 
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
0
@@ -7,7 +7,25 @@ class Site < ActiveRecord::Base
0
   
0
   # the fraction of campaigns filled for how many campaigns are available
0
   def saturation
0
- 0.75
0
+ self.campaigns_filled.to_f/self.campaigns_allowed.to_f
0
+ end
0
+
0
+ # number of campaigns allowed throughout the spots
0
+ def campaigns_allowed
0
+ ret = 0
0
+ self.spots.each do |spot|
0
+ ret += spot.campaign_limit
0
+ end
0
+ return ret
0
+ end
0
+
0
+ # number of campaigns currently running in the spots
0
+ def campaigns_filled
0
+ ret = 0
0
+ self.spots.each do |spot|
0
+ ret += spot.campaigns.count
0
+ end
0
+ return ret
0
   end
0
   
0
 end
0
\ No newline at end of file
...
7
8
9
10
11
 
 
 
 
 
12
13
14
...
7
8
9
 
 
10
11
12
13
14
15
16
17
0
@@ -7,7 +7,10 @@ class Spot < ActiveRecord::Base
0
   validates_numericality_of :price
0
   validates_uniqueness_of :name, :scope => :site_id
0
   
0
- cattr_accessor :pricing_types
0
- @@pricing_types = ["CPM", "CPC", "Monthly"]
0
+ # gets a campaign for a given index and returns false if none exists
0
+ def campaign_for(index)
0
+ return false if self.campaigns.count < index
0
+ return self.campaigns[index - 1]
0
+ end
0
   
0
 end
0
\ No newline at end of file
...
5
6
7
8
 
9
10
11
...
5
6
7
 
8
9
10
11
0
@@ -5,7 +5,7 @@
0
     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
0
     <link rel="stylesheet" href="/stylesheets/master.css" type="text/css" media="screen" charset="utf-8" />
0
     <%= js_include_tag "lib/mootools",
0
- "plugins/event_triggers", "plugins/mootools.extensions", "plugins/striper",
0
+ "plugins/event_triggers", "plugins/mootools.extensions", "plugins/striper", "plugins/date",
0
                         "application" %>
0
   </head>
0
   <body id="<%= controller_name %>_<%= action_name %>" class="section-<%= controller_name %>">
...
40
41
42
43
 
44
45
46
...
40
41
42
 
43
44
45
46
0
@@ -40,7 +40,7 @@
0
     </dl>
0
     <dl class="form">
0
       <dt><label>Pricing</label></dt>
0
- <dd><%= text_control :price %> <%= select_control :pricing_type, :collection => Spot.pricing_types %></dd>
0
+ <dd><%= text_control :price %> per month</dd>
0
     </dl>
0
     <dl class="form">
0
       <dt><label>Maximum Campaigns</label></dt>
...
5
6
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
9
10
...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
@@ -5,6 +5,24 @@ var rules = {
0
       if (this.value == null || this.value == "") return;
0
       document.location = "/campaigns/show/" + this.value;
0
     });
0
+ },
0
+
0
+ 'input.datepicker': function(element){
0
+ $(element).addEvent('keyup', function(){
0
+ var parsed = Date.parse(this.value);
0
+ var text = $(this.getAttribute('update_text'));
0
+ var field = $(this.getAttribute('update_field'));
0
+ if (parsed != null){
0
+ field.value = parsed.toString("yyyy-MM-dd HH:mm:ss");
0
+ text.innerHTML = parsed.toString("MMMM dd, yyyy");;
0
+ text.removeClass('invalid');
0
+ }else{
0
+ field.value = null;
0
+ text.innerHTML = "N/A";
0
+ text.addClass('invalid');
0
+ }
0
+ });
0
+ $(element).fireEvent('keyup');
0
   }
0
 }
0
 
...
168
169
170
 
 
 
171
172
173
...
183
184
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
187
188
...
168
169
170
171
172
173
174
175
176
...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
0
@@ -168,6 +168,9 @@ input[type=text], input[type=password]{
0
   border-right-color:#eaeaea;
0
   border-bottom-color:#eaeaea;
0
 }
0
+input[type=text].datepicker{
0
+ width:100px;
0
+}
0
 
0
 input[type=submit], button[type=submit]{
0
   padding:5px 10px;
0
@@ -183,6 +186,20 @@ input[type=submit], button[type=submit]{
0
   cursor:pointer;
0
 }
0
 
0
+p.dateresults{
0
+ margin-left:-20px;
0
+ font-size:11px;
0
+ color:#666;
0
+}
0
+p.dateresults strong{
0
+ color:#000;
0
+ padding-left:20px;
0
+}
0
+p.dateresults .invalid{
0
+ font-weight:bold;
0
+ color:#cc0000;
0
+}
0
+
0
 /*------------------------------------------------------------------------------------
0
   Sites
0
 ------------------------------------------------------------------------------------*/
...
3
4
5
6
7
8
 
9
10
11
...
3
4
5
 
6
7
8
9
10
11
0
@@ -3,9 +3,9 @@ class CampaignMigration < ActiveRecord::Migration
0
     create_table :campaigns do |t|
0
       t.string :name
0
       t.integer :weight
0
- t.integer :impression_limit
0
       t.datetime :starts_on
0
       t.datetime :ends_on
0
+ t.boolean :active
0
       t.integer :spot_id
0
       t.timestamps
0
     end
...
29
30
31
 
32
33
34
...
45
46
47
48
49
50
51
...
29
30
31
32
33
34
35
...
46
47
48
 
49
50
51
0
@@ -29,6 +29,7 @@ ActiveRecord::Schema.define(:version => 4) do
0
     t.integer "impression_limit"
0
     t.datetime "starts_on"
0
     t.datetime "ends_on"
0
+ t.boolean "active"
0
     t.integer "spot_id"
0
     t.datetime "created_at"
0
     t.datetime "updated_at"
0
@@ -45,7 +46,6 @@ ActiveRecord::Schema.define(:version => 4) do
0
     t.string "name"
0
     t.float "price"
0
     t.string "pricing_type"
0
- t.string "limit_type"
0
     t.integer "campaign_limit"
0
     t.integer "site_id"
0
     t.datetime "created_at"
...
41
42
43
44
45
46
47
48
49
50
51
52
53
...
41
42
43
 
 
 
 
 
 
 
 
44
45
0
@@ -41,12 +41,4 @@ describe Sites do
0
   
0
   it "should remove a site"
0
   
0
-end
0
-
0
-def valid_site_params(options = {})
0
- {:name => "Example Site", :url => "http://example.com"}.merge(options)
0
-end
0
-
0
-def valid_spot_params(options = {})
0
- {:name => "Example Spot", :price => 50.0, :pricing_type => "Monthly", :campaign_limit => 2}.merge(options)
0
 end
0
\ No newline at end of file
...
1
2
3
 
 
 
 
 
 
 
 
 
 
 
4
5
 
 
 
 
 
 
 
 
 
 
 
6
7
8
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
16
17
18
19
20
21
22
23
24
25
26
27
28
29
0
@@ -1,7 +1,28 @@
0
 require File.join( File.dirname(__FILE__), "..", "spec_helper" )
0
 
0
 describe Site do
0
+
0
+ before(:each) do
0
+ Site.destroy_all
0
+ Spot.destroy_all
0
+ Campaign.destroy_all
0
+
0
+ @example_site = Site.create(valid_site_params)
0
+ spot = Spot.create(valid_spot_params(:campaign_limit => 4))
0
+ @example_site.spots << spot
0
+ spot.campaigns << Campaign.create(valid_campaign_params)
0
+ end
0
 
0
- it "should have specs"
0
+ it "should count campaigns allowed" do
0
+ @example_site.campaigns_allowed.should == 4
0
+ end
0
+
0
+ it "should count campaigns filled" do
0
+ @example_site.campaigns_filled.should == 1
0
+ end
0
+
0
+ it "should calculate the saturation" do
0
+ @example_site.saturation.should == 0.25
0
+ end
0
 
0
 end
0
\ No newline at end of file
...
1
2
3
4
5
 
 
 
 
 
 
 
 
 
 
 
6
 
 
 
 
 
 
 
 
 
 
7
8
...
1
2
 
3
 
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
0
@@ -1,7 +1,26 @@
0
 require File.join( File.dirname(__FILE__), "..", "spec_helper" )
0
 
0
-describe Spot do
0
 
0
- it "should have specs"
0
+describe Spot do
0
+
0
+ before(:each) do
0
+ Spot.destroy_all
0
+ end
0
+
0
+ describe "with active campaigns" do
0
+
0
+ it "should have specs"
0
+
0
+ end
0
 
0
+ describe "without active campaigns" do
0
+
0
+ it "should return false on campaign_for" do
0
+ spot = Spot.create(valid_spot_params)
0
+ spot.campaign_for(1).should == false
0
+ spot.campaign_for(8).should == false
0
+ end
0
+
0
+ end
0
+
0
 end
0
\ No newline at end of file
...
9
10
11
 
 
 
 
 
 
 
 
 
 
 
 
12
...
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
0
@@ -9,3 +9,15 @@ Spec::Runner.configure do |config|
0
   config.include(Merb::Test::RouteHelper)
0
   config.include(Merb::Test::ControllerHelper)
0
 end
0
+
0
+def valid_site_params(options = {})
0
+ {:name => "Example Site", :url => "http://example.com"}.merge(options)
0
+end
0
+
0
+def valid_spot_params(options = {})
0
+ {:name => "Example Spot", :price => 50.0, :pricing_type => "Monthly", :campaign_limit => 2}.merge(options)
0
+end
0
+
0
+def valid_campaign_params(options ={})
0
+ {:name => "Example Campaign", :weight => 1.0, :starts_on => "2008-04-05 00:00:00", :ends_on => "2008-05-05 00:00:00"}
0
+end
0
\ No newline at end of file

Comments

    No one has commented yet.