diff --git a/app/controllers/application.rb b/app/controllers/application.rb index b0789ac..28e57d1 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -1,6 +1,14 @@ class ApplicationController < ActionController::Base session :session_key => '_barleysodas_session_id' + ## + # Ensures that the request was made using an Ajax request. + # + def ensure_xhr + return false unless request.xhr? + true + end + ## # Sane error and missing document messages. # diff --git a/app/controllers/beers_controller.rb b/app/controllers/beers_controller.rb index a97869b..184d7b7 100644 --- a/app/controllers/beers_controller.rb +++ b/app/controllers/beers_controller.rb @@ -34,6 +34,7 @@ class BeersController < ApplicationController # GET /beers/1;edit def edit @secondary_title = 'Update existing beer' + @brewery = @beer.brewery end # POST /beers @@ -42,6 +43,8 @@ class BeersController < ApplicationController @beer = Beer.new(params[:beer]) @page = Page.new(params[:page]) @beer.page = @page + brewery = Brewery.find_by_title(params[:brewery][:title]) rescue nil + @beer.brewery = brewery respond_to do |format| if @beer.save flash[:notice] = 'Beer was successfully created.' @@ -58,9 +61,11 @@ class BeersController < ApplicationController # PUT /beers/1 # PUT /beers/1.xml def update + @page.attributes = params[:page] + @beer.attributes = params[:beer] + brewery = Brewery.find_by_title(params[:brewery][:title]) rescue nil + @beer.brewery = brewery respond_to do |format| - @page.attributes = params[:page] - @beer.attributes = params[:beer] if @beer.save flash[:notice] = 'Beer was successfully updated.' format.html { redirect_to beer_url(@beer.page.title_for_url) } @@ -84,6 +89,9 @@ class BeersController < ApplicationController protected + ## + # Fetches the Beer and Page model for the actions. + # def get_beer_and_page @beer = Beer.find_by_title(Page.title_from_url(params[:id]), :include => [ 'page' ]) diff --git a/app/models/beer.rb b/app/models/beer.rb index 5727b68..5b5ad61 100644 --- a/app/models/beer.rb +++ b/app/models/beer.rb @@ -13,6 +13,9 @@ class Beer < ActiveRecord::Base # def page_attributes pattr = [] + unless brewery.nil? + pattr << "Brewery: #{brewery.title}" + end pattr << "ABV: #{"%.1f" % abv}%" unless abv.to_s.empty? unless original_gravity.to_s.empty? pattr << "Original Gravity: #{original_gravity}" diff --git a/app/views/beers/_beer_form.rhtml b/app/views/beers/_beer_form.rhtml index a54cfc6..421b314 100644 --- a/app/views/beers/_beer_form.rhtml +++ b/app/views/beers/_beer_form.rhtml @@ -10,4 +10,7 @@
<%= text_field 'beer', 'final_gravity' %>
++ <%= text_field_with_auto_complete('brewery', 'title', {}, { :url => { :controller => 'autocomplete', :action => 'index' } }) %> +
<%= render :partial => 'pages/page_form' %> diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index de585e8..c2338c0 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -34,4 +34,4 @@ pre { white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word; -} \ No newline at end of file +}