diff --git a/app/controllers/sites_cultivars_controller.rb b/app/controllers/sites_cultivars_controller.rb new file mode 100644 index 000000000..2600dc056 --- /dev/null +++ b/app/controllers/sites_cultivars_controller.rb @@ -0,0 +1,60 @@ +class SitesCultivarsController < ApplicationController + before_filter :login_required, :except => [ :show ] + + require 'csv' + + # GET /sites + # GET /sites.xml + def index + request.format = :xml if params[:format].nil? + + # We have a lot of params and they or may not have to be + # passed as conditions this is my way of adding them to conditions + # if need be + + conditions = {} + params.each do |k,v| + next if !SitesCultivars.column_names.include?(k) + conditions[k] = v + end + logger.info conditions.to_yaml + sitescultivars = SitesCultivars.where(conditions) + + respond_to do |format| + format.xml { render :xml => sitescultivars } + format.csv { render :csv => sitescultivars } + format.json { render :json => sitescultivars } + end + end + + # GET /cultivars/new + # GET /cultivars/new.xml + def new + sitescultivars = SitesCultivars.new + + respond_to do |format| + format.xml { render :xml => sitescultivars } + format.csv { render :csv => sitescultivars } + format.json { render :json => sitescultivars } + end + end + + # POST /cultivars + # POST /cultivars.xml + def create + sitescultivars = SitesCultivars.new(params[:sitescultivars]) + + respond_to do |format| + if citationssites.save + format.xml { render :xml => sitescultivars, :status => :created } + format.csv { render :csv => sitescultivars, :status => :created } + format.json { render :json => sitescultivars, :status => :created } + else + format.xml { render :xml => sitescultivars.errors, :status => :unprocessable_entity } + format.csv { render :csv => sitescultivars.errors, :status => :unprocessable_entity } + format.json { render :json => sitescultivars.errors, :status => :unprocessable_entity } + end + end + end + +end diff --git a/app/models/cultivar.rb b/app/models/cultivar.rb index e19ce5309..40884e8b9 100644 --- a/app/models/cultivar.rb +++ b/app/models/cultivar.rb @@ -7,9 +7,12 @@ class Cultivar < ActiveRecord::Base SEARCH_INCLUDES = %w{ specie } SEARCH_FIELDS = %w{ species.scientificname cultivars.previous_id cultivars.name cultivars.ecotype cultivars.notes } + has_many :sites_cultivars, :class_name => "SitesCultivars" + has_many :sites, :through => :sites_cultivars + has_many :traits has_many :yields - + belongs_to :specie validates :name, diff --git a/app/models/site.rb b/app/models/site.rb index e41690137..ef7bcf46c 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -23,6 +23,9 @@ class Site < ActiveRecord::Base has_many :experiments_sites has_many :experiments, :through => :experiments_sites + + has_many :sites_cultivars, :class_name => "SitesCultivars" + has_many :cultivars, :through => :sites_cultivars has_many :yields has_many :traits diff --git a/app/models/sites_cultivars.rb b/app/models/sites_cultivars.rb new file mode 100644 index 000000000..ac706ea5b --- /dev/null +++ b/app/models/sites_cultivars.rb @@ -0,0 +1,16 @@ +class SitesCultivars < ActiveRecord::Base + self.primary_key = "id" + + validates_presence_of :cultivar_id + validates_presence_of :site_id + + belongs_to :cultivar + belongs_to :site + + comma do + cultivar_id + site_id + created_at + updated_at + end +end diff --git a/config/routes.rb b/config/routes.rb index 9e03e72fe..80be61605 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -171,6 +171,8 @@ end end + resources :sites_cultivars + resources :citations do member do get :search_sites