Skip to content

Instantly share code, notes, and snippets.

@MartinMcDermid
Created December 5, 2013 01:43
Show Gist options
  • Save MartinMcDermid/7798831 to your computer and use it in GitHub Desktop.
Save MartinMcDermid/7798831 to your computer and use it in GitHub Desktop.
class LotteriesController < ApplicationController
def index
@lotteries = Lottery.paginate(:page => params[:page], :per_page => 5, :order => 'created_at desc, id')
# @lotteries = Lottery.find(:all, :conditions => ["completed = ?", false])
@completed_lotteries = Lottery.find(:all, :conditions => ["completed = ?", true])
#@tickets = Ticket.find(:all, :conditions => ["lottery_id = ?", params[:id]])
#@tickets = Ticket.all
end
def show
@lottery = Lottery.find(params[:id])
@tickets = Ticket.find(:all, :conditions => ["lottery_id = ?", params[:id]])
end
def new
@lottery = Lottery.new
end
def create
@lottery = Lottery.new(params[:lottery])
@product_tickets = @lottery.product.tickets
@product_tickets.times { @tickets = @lottery.tickets.build }
if @lottery.save
redirect_to @lottery, :notice => "Successfully created lottery."
else
render :action => 'new'
end
end
def edit
@lottery = Lottery.find(params[:id])
end
def update
@lottery = Lottery.find(params[:id])
if @lottery.update_attributes(params[:lottery])
redirect_to @lottery, :notice => "Successfully updated lottery."
else
render :action => 'edit'
end
end
def destroy
@lottery = Lottery.find(params[:id])
@lottery.destroy
redirect_to lotteries_url, :notice => "Successfully destroyed lottery."
end
def join
@lottery = Lottery.find(params[:id])
@ticket = Ticket.find(params[:id])
@ticket.update_attribute(:user_id, current_user.id)
puts @ticket.id
redirect_to @lottery
end
def leave
@lottery = Lottery.find params[:id]
current_user.update_attribute(:team_id, nil)
redirect_to @lottery
end
end
Blink::Application.routes.draw do
# The priority is based upon order of creation:
# first created -> highest priority.
resources :categories
resources :products do
resources :tickets
end
#resources :lotteries
resources :lotteries do
member do
get 'join'
get 'leave'
end
resources :tickets
end
resources :tickets
authenticated :user do
root :to => 'home#index'
end
devise_for :users do
get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session
end
resources :users
root :to => "home#index"
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => 'welcome#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
end
class Product < ActiveRecord::Base
attr_accessible :name, :url, :cost, :postage_cost, :category_id, :token_cost, :tickets, :rrp
belongs_to :category
has_and_belongs_to_many :lotteries
end
class Ticket < ActiveRecord::Base
attr_accessible :lottery_id, :user_id, :win
belongs_to :lottery
has_many :users, through: :tickets
end
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :role_ids, :as => :admin
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
has_many :tickets, through: :lotteries
has_many :lotteries
end
class Lottery < ActiveRecord::Base
attr_accessible :product_id, :completed, :tickets_attributes
belongs_to :product
has_many :tickets
accepts_nested_attributes_for :tickets, :allow_destroy => true
end
<div id="active_lotteries">
<% for lottery in @lotteries %>
<div id="lottery">
<div class="left-tickets col-md-3">
<% lottery.tickets[0..4].each do |ticket| %>
<ul>
<% if ticket.user_id.nil? %>
<%= link_to 'join', join_lottery_path(lottery) %>
<% else %>
<li><a class="closed" href="#"><%= ticket.user_id %> closed</a></li>
<% end %>
</ul>
<% end %>
</div> <!-- left tickets -->
<div class="lottery-info col-md-6">
<p><%= link_to lottery.product.name, lottery %></p>
<img src="http://placehold.it/530x200" class="img-responsive" alt="Responsive image">
</div>
<div class="right-tickets col-md-3">
<% lottery.tickets[5..9].each do |ticket| %>
<ul>
<% if ticket.user_id.nil? %>
<li><a class="open" href="#"><%= ticket.id %> open</a></li>
<% else %>
<li><a class="closed" href="#"><%= ticket.user_id %> closed</a></li>
<% end %>
</ul>
<% end %>
</div> <!-- right tickets -->
</div> <!-- lottery -->
<% end %>
</div> <!-- active lotteries -->
<div id="recently-completed">
<a href="#" onclick="return false;"><img class="drop" src="assets/drop-down.png"></a><p>Recently Completed Blinks</p>
</div> <!-- recently completed -->
<%= will_paginate @lotteries, renderer: BootstrapPagination::Rails %>
<script>
$( "img.drop" ).click(function() {
$( ".completed" ).toggle( "slow" );
});
</script>
<!--
<div class="navbar navbar-default navbar-fixed-bottom">
<div class="container footer">
<%= link_to 'Create new Lottery', new_lottery_path, :method => :delete, :confirm => 'Are you sure?', :class => 'navbar-btn btn-warning btn pull-right' %>
</div>
</div>
-->
<%= link_to 'Create new Lottery', new_lottery_path, :method => :delete, :confirm => 'Are you sure?', :class => 'navbar-btn btn-warning btn pull-right' %>
<p></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment