From a06dc84eb0948082cab74fedd2ead38723b4f37d Mon Sep 17 00:00:00 2001 From: shlok007 Date: Wed, 31 May 2017 18:42:52 +0530 Subject: [PATCH 1/3] introduce organizations --- app/models/conference.rb | 5 ++++- app/models/organization.rb | 7 +++++++ config/routes.rb | 2 +- .../20170529215453_create_organizations.rb | 9 +++++++++ ...094817_add_organizaton_id_to_conference.rb | 5 +++++ db/schema.rb | 9 ++++++++- .../organizations_controller_spec.rb | 0 spec/factories/organizations.rb | 13 ++++++++++++ spec/models/organization_spec.rb | 20 +++++++++++++++++++ 9 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 app/models/organization.rb create mode 100644 db/migrate/20170529215453_create_organizations.rb create mode 100644 db/migrate/20170531094817_add_organizaton_id_to_conference.rb create mode 100644 spec/controllers/organizations_controller_spec.rb create mode 100644 spec/factories/organizations.rb create mode 100644 spec/models/organization_spec.rb diff --git a/app/models/conference.rb b/app/models/conference.rb index 1c4ccc73..523ca8cc 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -7,6 +7,8 @@ class Conference < ActiveRecord::Base default_scope { order('start_date DESC') } + belongs_to :organization + has_paper_trail ignore: %i(updated_at guid revision events_per_week), meta: { conference_id: :id } has_and_belongs_to_many :questions @@ -53,7 +55,8 @@ class Conference < ActiveRecord::Base :start_date, :end_date, :start_hour, - :end_hour, presence: true + :end_hour, + :organization, presence: true validates :short_title, uniqueness: true validates :short_title, format: { with: /\A[a-zA-Z0-9_-]*\z/ } diff --git a/app/models/organization.rb b/app/models/organization.rb new file mode 100644 index 00000000..87accd1e --- /dev/null +++ b/app/models/organization.rb @@ -0,0 +1,7 @@ +class Organization < ActiveRecord::Base + has_many :conferences, dependent: :destroy + + validates :name, presence: true + + mount_uploader :picture, PictureUploader, mount_on: :picture +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9fa1c29a..645dcf8b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -102,7 +102,7 @@ Osem::Application.routes.draw do get '/revision_history/:id/revert_object' => 'versions#revert_object', as: 'revision_history_revert_object' get '/revision_history/:id/revert_attribute' => 'versions#revert_attribute', as: 'revision_history_revert_attribute' end - + resources :organizations resources :conferences, only: [:index, :show] do resource :program, only: [] do resources :proposals, except: :destroy do diff --git a/db/migrate/20170529215453_create_organizations.rb b/db/migrate/20170529215453_create_organizations.rb new file mode 100644 index 00000000..11caf736 --- /dev/null +++ b/db/migrate/20170529215453_create_organizations.rb @@ -0,0 +1,9 @@ +class CreateOrganizations < ActiveRecord::Migration + def change + create_table :organizations do |t| + t.string :name + t.text :description + t.string :picture + end + end +end diff --git a/db/migrate/20170531094817_add_organizaton_id_to_conference.rb b/db/migrate/20170531094817_add_organizaton_id_to_conference.rb new file mode 100644 index 00000000..4397ed7a --- /dev/null +++ b/db/migrate/20170531094817_add_organizaton_id_to_conference.rb @@ -0,0 +1,5 @@ +class AddOrganizatonIdToConference < ActiveRecord::Migration + def change + add_column :conferences, :organization_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index ab95b4dc..8257150a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170302145716) do +ActiveRecord::Schema.define(version: 20170531094817) do create_table "ahoy_events", force: :cascade do |t| t.uuid "visit_id", limit: 16 @@ -100,6 +100,7 @@ ActiveRecord::Schema.define(version: 20170302145716) do t.string "picture" t.integer "start_hour", default: 9 t.integer "end_hour", default: 20 + t.integer "organization_id" end create_table "conferences_questions", id: false, force: :cascade do |t| @@ -266,6 +267,12 @@ ActiveRecord::Schema.define(version: 20170302145716) do t.datetime "updated_at" end + create_table "organizations", force: :cascade do |t| + t.string "name" + t.text "description" + t.string "picture" + end + create_table "payments", force: :cascade do |t| t.string "last4" t.integer "amount" diff --git a/spec/controllers/organizations_controller_spec.rb b/spec/controllers/organizations_controller_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/spec/factories/organizations.rb b/spec/factories/organizations.rb new file mode 100644 index 00000000..69829de7 --- /dev/null +++ b/spec/factories/organizations.rb @@ -0,0 +1,13 @@ +FactoryGirl.define do + factory :organization do + name { Faker::Company.name } + description { Faker::Lorem.paragraph } + + # after(:create) do |organization| + # File.open("spec/support/logos/#{1 + rand(13)}.png") do |file| + # organization.picture = file + # end + # organization.save! + # end + end +end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb new file mode 100644 index 00000000..45a50827 --- /dev/null +++ b/spec/models/organization_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe Organization do + let(:organization) { create(:organization) } + + describe 'validation' do + it 'has a valid factory' do + expect( build(:organization) ).to be_valid + end + + it 'is not valid without a name' do + should validate_presence_of(:name) + end + end + + describe 'associations' do + it { should have_many(:conferences).dependent(:destroy) } + end +end + From b1fac334205dda2e2a8a22b714f84afb2e695675 Mon Sep 17 00:00:00 2001 From: shlok007 Date: Wed, 31 May 2017 19:31:42 +0530 Subject: [PATCH 2/3] Introduce basic views for organization organization#index organizaton#new and organization#edit --- app/controllers/organizations_controller.rb | 41 +++++++++++++++++++++ app/views/organizations/_form.html.haml | 13 +++++++ app/views/organizations/edit.html.haml | 4 ++ app/views/organizations/index.html.haml | 17 +++++++++ app/views/organizations/new.html.haml | 4 ++ 5 files changed, 79 insertions(+) create mode 100644 app/controllers/organizations_controller.rb create mode 100644 app/views/organizations/_form.html.haml create mode 100644 app/views/organizations/edit.html.haml create mode 100644 app/views/organizations/index.html.haml create mode 100644 app/views/organizations/new.html.haml diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb new file mode 100644 index 00000000..28016899 --- /dev/null +++ b/app/controllers/organizations_controller.rb @@ -0,0 +1,41 @@ +class OrganizationsController < ApplicationController + load_and_authorize_resource :organization + + def index + @organizations = Organization.all + end + + def create + @organization = Organization.new(organization_params) + if @organization.save + redirect_to organizations_path, + notice: 'Organization successfully created' + else + redirect_to new_organization_path, + error: @organization.errors.full_messages.join(', ') + end + end + + def new + @organization = Organization.new + end + + def edit + end + + def update + if @organization.update_attributes(organization_params) + redirect_to organizations_path, + notice: 'Organization successfully updated' + else + redirect_to edit_organization_path(@organization), + error: @organization.errors.full_messages.join(', ') + end + end + + private + + def organization_params + params.require(:organization).permit(:name, :description, :picture) + end +end diff --git a/app/views/organizations/_form.html.haml b/app/views/organizations/_form.html.haml new file mode 100644 index 00000000..c41f0cff --- /dev/null +++ b/app/views/organizations/_form.html.haml @@ -0,0 +1,13 @@ += semantic_form_for(@organization) do |f| + = f.inputs name: 'Organization details' do + = f.input :name, as: :string, required: true + = f.input :description, input_html: { rows: 5 }, placeholder: 'Decribe about your organization..' + = image_tag f.object.picture.thumb.url if f.object.picture? + - if @organization.picture + = image_tag(@organization.picture.thumb.url, width: '20%') + = f.input :picture + %p.text-right + - if @organization.new_record? + = f.submit 'Create Organization', class: 'btn btn-success' + - else + = f.submit 'Update Organization', class: 'btn btn-success' \ No newline at end of file diff --git a/app/views/organizations/edit.html.haml b/app/views/organizations/edit.html.haml new file mode 100644 index 00000000..1f65d485 --- /dev/null +++ b/app/views/organizations/edit.html.haml @@ -0,0 +1,4 @@ +.container + .row + .col-md-12 + = render 'form' diff --git a/app/views/organizations/index.html.haml b/app/views/organizations/index.html.haml new file mode 100644 index 00000000..9defd933 --- /dev/null +++ b/app/views/organizations/index.html.haml @@ -0,0 +1,17 @@ +.container + .row + .col-md-12.page-header + %h1 + Organizations + .btn-group.pull-right + = link_to 'Add new', new_organization_path, class: 'btn btn-mini btn-success' + - @organizations.each do |organization| + .col-md-4 + .thumbnail + = image_tag(organization.picture.thumb.url, width: '20%') + .caption + %h4 + = organization.name + %button.btn.btn-success Know More + = link_to 'Edit', edit_organization_path(organization), class: 'btn btn-mini btn-default' + diff --git a/app/views/organizations/new.html.haml b/app/views/organizations/new.html.haml new file mode 100644 index 00000000..1f65d485 --- /dev/null +++ b/app/views/organizations/new.html.haml @@ -0,0 +1,4 @@ +.container + .row + .col-md-12 + = render 'form' From ae1f292719fcc8ef323991ad116fa90f914bc92d Mon Sep 17 00:00:00 2001 From: shlok007 Date: Wed, 31 May 2017 19:35:43 +0530 Subject: [PATCH 3/3] indent using spaces --- app/controllers/organizations_controller.rb | 63 ++++++++++----------- app/models/organization.rb | 2 +- spec/factories/organizations.rb | 16 +++--- spec/models/organization_spec.rb | 3 +- 4 files changed, 41 insertions(+), 43 deletions(-) diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 28016899..13d6c204 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -1,41 +1,40 @@ class OrganizationsController < ApplicationController - load_and_authorize_resource :organization + load_and_authorize_resource :organization - def index - @organizations = Organization.all - end + def index + @organizations = Organization.all + end - def create - @organization = Organization.new(organization_params) - if @organization.save - redirect_to organizations_path, - notice: 'Organization successfully created' - else - redirect_to new_organization_path, - error: @organization.errors.full_messages.join(', ') - end - end + def create + @organization = Organization.new(organization_params) + if @organization.save + redirect_to organizations_path, + notice: 'Organization successfully created' + else + redirect_to new_organization_path, + error: @organization.errors.full_messages.join(', ') + end + end - def new - @organization = Organization.new - end + def new + @organization = Organization.new + end - def edit - end + def edit; end - def update - if @organization.update_attributes(organization_params) - redirect_to organizations_path, - notice: 'Organization successfully updated' - else - redirect_to edit_organization_path(@organization), - error: @organization.errors.full_messages.join(', ') - end - end + def update + if @organization.update_attributes(organization_params) + redirect_to organizations_path, + notice: 'Organization successfully updated' + else + redirect_to edit_organization_path(@organization), + error: @organization.errors.full_messages.join(', ') + end + end - private + private - def organization_params - params.require(:organization).permit(:name, :description, :picture) - end + def organization_params + params.require(:organization).permit(:name, :description, :picture) + end end diff --git a/app/models/organization.rb b/app/models/organization.rb index 87accd1e..60a83fef 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -4,4 +4,4 @@ class Organization < ActiveRecord::Base validates :name, presence: true mount_uploader :picture, PictureUploader, mount_on: :picture -end \ No newline at end of file +end diff --git a/spec/factories/organizations.rb b/spec/factories/organizations.rb index 69829de7..0ee76554 100644 --- a/spec/factories/organizations.rb +++ b/spec/factories/organizations.rb @@ -1,13 +1,13 @@ FactoryGirl.define do factory :organization do - name { Faker::Company.name } - description { Faker::Lorem.paragraph } + name { Faker::Company.name } + description { Faker::Lorem.paragraph } - # after(:create) do |organization| - # File.open("spec/support/logos/#{1 + rand(13)}.png") do |file| - # organization.picture = file - # end - # organization.save! - # end + # after(:create) do |organization| + # File.open("spec/support/logos/#{1 + rand(13)}.png") do |file| + # organization.picture = file + # end + # organization.save! + # end end end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 45a50827..748205d5 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -5,7 +5,7 @@ describe Organization do describe 'validation' do it 'has a valid factory' do - expect( build(:organization) ).to be_valid + expect(build(:organization)).to be_valid end it 'is not valid without a name' do @@ -17,4 +17,3 @@ describe Organization do it { should have_many(:conferences).dependent(:destroy) } end end -