/company.rb Secret
Created
November 18, 2012 01:37
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Company < ActiveRecord::Base | |
set_primary_key :nip | |
attr_accessible :address_number, :address_postalcode, :address_street, | |
:application_type, :company_url, :name, :nip, :trade, | |
:admin_id | |
has_many :jobs | |
has_many :employments | |
has_many :employers, through: :employments | |
validates :nip, | |
nip: true, | |
presence: true, | |
uniqueness: true | |
validates :name, | |
presence: true | |
validates :admin_id, | |
presence: true | |
def admin | |
employers.find(admin_id) | |
end | |
end | |
# == Schema Information | |
# | |
# Table name: companies | |
# | |
# address_number :string(255) | |
# address_postalcode :string(255) | |
# address_street :string(255) | |
# admin_id :integer | |
# application_type :string(255) | |
# company_url :string(255) | |
# created_at :datetime not null | |
# name :string(255) | |
# nip :integer primary key | |
# updated_at :datetime not null | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employer < ActiveRecord::Base | |
attr_accessible :email, :first_name, :last_name, :phone_number | |
has_many :employments | |
has_many :companies, through: :employments | |
has_many :jobs | |
validates :first_name, | |
presence: true | |
validates :email, | |
presence: true | |
end | |
# == Schema Information | |
# | |
# Table name: employers | |
# | |
# created_at :datetime not null | |
# email :string(255) | |
# first_name :string(255) | |
# id :integer not null, primary key | |
# last_name :string(255) | |
# phone_number :string(255) | |
# updated_at :datetime not null | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employment < ActiveRecord::Base | |
attr_accessible :company_id, :created_at, :employer_id | |
belongs_to :company | |
belongs_to :employer | |
end | |
# == Schema Information | |
# | |
# Table name: employments | |
# | |
# company_id :integer | |
# created_at :datetime not null | |
# employer_id :integer | |
# id :integer not null, primary key | |
# updated_at :datetime not null | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Job < ActiveRecord::Base | |
attr_accessible :additional_description, :address_city, :address_number, :address_postalcode, | |
:address_street, :application_address, :application_date, :employer_id, | |
:position, :requirements, :job_description, :trade_id, :company_id | |
attr_accessible :company_attributes, :employer_attributes | |
belongs_to :company | |
belongs_to :employer | |
belongs_to :trade | |
accepts_nested_attributes_for :company, :employer | |
validates :position, | |
presence: true | |
validates :trade_id, | |
presence: true | |
def self.create_with_company(job_params) | |
company_attributes = job_params.delete(:company_attributes) | |
employer_attributes = job_params.delete(:employer_attributes) | |
new(job_params) do |job| | |
job.employer = Employer.find_or_create_by_email(employer_attributes) | |
company_attributes[:admin_id] = job.employer.id if Company.find_by_nip(company_attributes[:nip]).nil? | |
job.company = Company.find_or_create_by_nip(company_attributes) | |
Employment.create(employer_id: job.employer.id, company_id: job.company.id) | |
end | |
end | |
end | |
# == Schema Information | |
# | |
# Table name: jobs | |
# | |
# additional_description :text | |
# application_date :date | |
# company_id :integer | |
# created_at :datetime not null | |
# employer_id :integer | |
# id :integer not null, primary key | |
# job_description :text | |
# position :string(255) | |
# requirements :text | |
# trade_id :integer | |
# updated_at :datetime not null | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JobsController < ApplicationController | |
def index | |
@jobs = Job.all | |
end | |
def new | |
@job = Job.new | |
@job.company = Company.new | |
@job.employer = Employer.new | |
end | |
def create | |
@job = Job.create_with_company(params[:job]) | |
if @job.save | |
redirect_to jobs_path, | |
notice: t('activerecord.successful.messages.created', model: @job.class.model_name.human) | |
else | |
render 'new' | |
end | |
end | |
def edit | |
@job = Job.find(params[:id]) | |
end | |
def update | |
@job = Job.find(params[:id]) | |
if @job.update_attributes(params[:job]) | |
redirect_to jobs_path, | |
notice: t('activerecord.successful.messages.created', model: @job.class.model_name.human) | |
else | |
render 'edit' | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= simple_form_for @job, html: { multipart: true, class: 'form-horizontal' } do |f| | |
= f.simple_fields_for :company do |c| | |
= c.input :nip | |
= c.input :name | |
= c.input :address_street | |
= f.input :trade_id, :collection => Trade.all | |
= f.input :job_description | |
= f.input :position | |
= f.input :requirements | |
= f.simple_fields_for :employer do |e| | |
= e.input :first_name | |
= e.input :last_name | |
= e.input :email | |
= f.submit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment