Report abuse

my take of the 'resources' (I anticipate searching on a lot of these as stock grows...)

001_create_airrifles.rb      008_create_airpistols.rb   015_create_cartridges.rb
002_create_firearms.rb       009_create_ammunitions.rb  016_create_barrels.rb
003_create_shotguns.rb       010_create_scopes.rb       017_create_descriptions.rb
004_create_calibres.rb       011_create_chamberings.rb  018_create_mechanisms.rb
005_create_conditions.rb     012_create_models.rb       019_create_assets.rb
006_create_manufacturers.rb  013_create_galleries.rb
007_create_disciplines.rb    014_create_gauges.rb




class FirearmsController < ApplicationController

  def index
    @manufacturers = Manufacturer.find(:all, :conditions => {:kind => 2}, :order => 'title asc')
    @kind = 2
  end

  def show
    @firearm = Firearm.find(params[:id])
  end

  def new
    @firearm = Firearm.new
    @manufacturer = Manufacturer.new
    @model = Model.new
    @chambering = Chambering.new
    @calibre = Calibre.new
    @gallery = Gallery.new
    @kind = 2
  end

  def edit
    @firearm = Firearm.find(params[:id])
    @manufacturer = Manufacturer.find(params[:id])
    @model = Model.find(params[:id])
    @chambering = Chambering.new
    @calibre = Calibre.new
    @gallery = Gallery.new
    @kind = 2
  end

  def create
    @firearm = Firearm.new(params[:firearm])
    if @firearm.save
      if @firearm.gallery_id == 0  
  @gallery = Gallery.create(:model_id => @firearm.model_id, :kind => 2, :title => @firearm.title)
  Firearm.update(@firearm, :gallery_id => @gallery.id)
      end
      flash[:notice] = "Added Firearm"
      redirect_to new_firearm_path
    else
      render :action => 'new'
    end
   end

  def update
    @firearm = Firearm.find(params[:id])

    respond_to do |format|
      if @firearm.update_attributes(params[:firearm])
        flash[:notice] = 'Gun was successfully updated.'
        format.html { redirect_to(@firearm) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @firearm.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @firearm = Firearm.find(params[:id])
    @firearm.destroy

    respond_to do |format|
      format.html { redirect_to(firearms_url) }
      format.xml  { head :ok }
    end
  end

end


views/firearms/edit

%h1== Edit firearm #{flash[:notice]}
=render :partial => 'form'
#create_associated
  =render :partial => 'galleries/new'

views/firearms/new - same form partial as for the new template, rails fills in the values 
if the record already exists.

%h1== Add a firearm #{flash[:notice]}
=render :partial => 'form'
#create_associated
  =render :partial => 'galleries/new'
  =render :partial => 'manufacturers/new'
  =render :partial => 'models/new'
  =render :partial => 'calibres/new'
  =render :partial => 'chamberings/new'


views/chamberings/_new

-form_for(@chambering) do |f|
  =label('chambering','Add a new chambering')
  =f.text_field :title
  =submit_tag 'Create'

 -- I don't have a views/chamberings/new template as I anticipate only adding to these models
 via the main products pages, so created a single partial I can share around.

 the controllers for these ancillary models redirects ':back' to the main product page -- 
since I on;y anticipate updating them from one of the gun categories. Keeps things sorta 
restful (?) without resorting to some uber products controllers.

class ChamberingsController < ApplicationController

  def index
    @manufacturers = Manufacturer.find(:all, :conditions => {:kind => 1}, :order => 'title asc')
  end

  def show
    @chambering = Chambering.find(params[:id])
  end

  def new
    @chambering = Chambering.new
    @manufacturer = Manufacturer.new
    @model = Model.new
    @kind = 1

  end

  def edit
    @chambering = Chambering.find(params[:id])
    @manufacturer = Manufacturer.find(params[:id])
    @model = Model.find(params[:id])
    @kind = 1
  end

  def create
    @chambering = Chambering.new(params[:chambering])
    if @chambering.save
      flash[:notice] = "Added Chambering"
      redirect_to :back
    else
      render :action => 'new'
    end
   end

  def update
    @chambering = Chambering.find(params[:id])

    respond_to do |format|
      if @chambering.update_attributes(params[:chambering])
        flash[:notice] = 'Gun was successfully updated.'
        format.html { redirect_to(@chambering) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @chambering.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @chambering = Chambering.find(params[:id])
    @chambering.destroy

    respond_to do |format|
      format.html { redirect_to(chamberings_url) }
      format.xml  { head :ok }
    end
  end

end


Main product 'cms' form, very similar for all main product types:

views/firearms/_form

-form_for(@firearm) do |f|
  %p
    =label('our_ref','Our Ref')
    =f.text_field :title
  %p{:class => 'clear', :id => 'make_select'}
    =label('manufacturer','make')
    =f.collection_select(:manufacturer_id, Manufacturer.find(:all, :order => 'title ASC', :conditions => {:kind => 2}), :id, :title)
  %p
    =label('model','model')
    %select{:id => "model_select", :name => "firearm[model_id]"}
      -Model.find(:all, :order => 'id ASC', :conditions => {:kind => 2}).each do |m|
        %option{:label => m.manufacturer_id, :value => m.id}= m.title
  %p{:class => 'clear'}
    =label('calibre','calibre')
    =f.collection_select(:calibre_id, Calibre.find(:all, :conditions => {:kind => 2}), :id, :title)
  %p
    =label('chambering','chambering')
    =f.collection_select(:chambering_id, Chambering.find(:all), :id, :title)
  %p
    =label('barrel','barrel')
    =f.collection_select(:barrel_id, Barrel.find(:all, :conditions => {:kind => 1}), :id, :title)
  %p{:class => 'clear'}
    =label('condition', 'condition')
    =f.collection_select(:condition_id, Condition.find(:all), :id, :title)
  %p
    =label('lefthanded','Left handed')
    %select{:name => "firearm[left_handed]"}
      %option{:value => 0, :selected => 'selected'}No
      %option{:value => 1}Yes
  %p{:class => 'clear'}
    =label('description', 'description')
    =f.text_area :description, :size => '40x5'
  %p
    =label('price', 'price')
    =f.text_field :price, :size => 4
  %p{:class => 'clear'}
    =label('gallery', 'gallery')
    %select{:id => "gallery_select", :name => "firearm[gallery_id]"}
      %option{:value => 0, :label => 0}New Gallery for this rifle
      -Gallery.find(:all, :order => 'id ASC', :conditions => {:kind => 2}).each do |g|
        %option{:label => g.model_id, :value => g.id}= g.title
  %p
    =submit_tag "Save"
#gallery_preview
=javascript_include_tag 'add_edit'






stuff like this is really dumb --

    =label('lefthanded','Left handed')
    %select{:name => "firearm[left_handed]"}
      %option{:value => 0, :selected => 'selected'}No
      %option{:value => 1}Yes

    f.check_box(:left_handed) is all I needed.