From 465976bc60eb570249f8d4b67b019bb220a05ea2 Mon Sep 17 00:00:00 2001 From: Rishabh Singh Date: Thu, 13 Jun 2019 23:45:05 +0530 Subject: [PATCH] Add contactable to tracks Added social media fields to track submission form by using a polymorphic association with the Contact Model. --- app/controllers/tracks_controller.rb | 9 +++++++-- app/models/contact.rb | 1 + app/models/track.rb | 2 ++ app/views/tracks/_form.html.haml | 11 +++++++++++ .../20190612161235_add_polymorphic_to_contact.rb | 6 ++++++ db/schema.rb | 4 +++- spec/controllers/tracks_controller_spec.rb | 1 + 7 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20190612161235_add_polymorphic_to_contact.rb diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index ad418d46c2..d0188e7e08 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -13,12 +13,15 @@ def show; end def new @track = @program.tracks.new(color: @conference.next_color_for_collection(:tracks)) + @track.build_contact end def edit; end def create @track = @program.tracks.new(track_params) + @contact = @track.build_contact(track_params[:contact_attributes]) + @contact.conference = @conference @track.submitter = current_user @track.cfp_active = false if @track.save @@ -35,7 +38,8 @@ def update redirect_to conference_program_tracks_path(conference_id: @conference.short_title), notice: 'Track request successfully updated.' else - flash.now[:error] = "Track request update failed: #{@track.errors.full_messages.join('. ')}." + flash.now[:error] = "Track request update failed: #{@track.errors.full_messages.join('. ')}" \ + "#{@track.contact.errors.full_messages.join('. ')}." render :edit end end @@ -55,7 +59,8 @@ def withdraw private def track_params - params.require(:track).permit(:name, :description, :color, :short_name, :start_date, :end_date, :relevance) + params.require(:track).permit(:name, :description, :color, :short_name, :start_date, :end_date, :relevance, + contact_attributes: [:id, :social_tag, :facebook, :googleplus, :twitter, :instagram, :mastodon, :youtube]) end def update_state(transition, notice) diff --git a/app/models/contact.rb b/app/models/contact.rb index e69f9dbbcb..40d2231373 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -4,6 +4,7 @@ class Contact < ApplicationRecord has_paper_trail on: [:update], ignore: [:updated_at], meta: { conference_id: :conference_id } belongs_to :conference + belongs_to :contactable, polymorphic: true validates :conference, presence: true # Conferences only have one contact diff --git a/app/models/track.rb b/app/models/track.rb index d34bfbefcc..05ef5bfc52 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -12,6 +12,8 @@ class Track < ApplicationRecord belongs_to :selected_schedule, class_name: 'Schedule' has_many :events, dependent: :nullify has_many :schedules + has_one :contact, as: :contactable + accepts_nested_attributes_for :contact has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id } diff --git a/app/views/tracks/_form.html.haml b/app/views/tracks/_form.html.haml index 42832e8051..dcd9a6bc97 100644 --- a/app/views/tracks/_form.html.haml +++ b/app/views/tracks/_form.html.haml @@ -17,4 +17,15 @@ = f.input :end_date, as: :string, input_html: { id: 'registration-period-end-datepicker', readonly: 'readonly' } = f.input :description, input_html: {rows: 2, data: { provide: 'markdown-editable' } }, required: true, hint: "This will be public #{markdown_hint}".html_safe = f.input :relevance, input_html: {rows: 5, data: { provide: 'markdown-editable' } }, required: true, hint: "Please explain here how this track relates to the conference, how you are related to its content and why we should accept it. #{markdown_hint}".html_safe + + = f.inputs name:'Social Media' do + = f.semantic_fields_for :contact, @track.contact do |c| + = c.input :social_tag, hint: "The hashtag you'll use on Twitter and Google+. Don't include the '#' sign!'", required: false + = c.input :facebook, hint: 'This will appear in the social media section as a link to the Facebook.', required: false + = c.input :googleplus, label: 'Google+ Url', hint: 'This will appear in the social media section as a link to the Google+ Page of your Track', required: false + = c.input :twitter, hint: 'This will appear in the social media section as a link to the Twitter Page of your Track', required: false + = c.input :instagram, hint: 'This will appear in the social media section as a link to the Instagram Page of your Track', required: false + = c.input :mastodon, hint: 'This will appear in the social media section as a link to the Mastodon Page of your Track', required: false + = c.input :youtube, hint: 'This will appear in the social media section as a link to the YouTube channel for your Track.', required: false + = f.action :submit, as: :button, button_html: { class: 'btn btn-primary' } diff --git a/db/migrate/20190612161235_add_polymorphic_to_contact.rb b/db/migrate/20190612161235_add_polymorphic_to_contact.rb new file mode 100644 index 0000000000..502fee3033 --- /dev/null +++ b/db/migrate/20190612161235_add_polymorphic_to_contact.rb @@ -0,0 +1,6 @@ +class AddPolymorphicToContact < ActiveRecord::Migration[5.1] + def change + add_column :contacts, :contactable_type, :string + add_column :contacts, :contactable_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index b199367e0b..683eb43708 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_06_03_143107) do +ActiveRecord::Schema.define(version: 2019_06_12_161235) do create_table "answers", force: :cascade do |t| t.string "title" @@ -126,6 +126,8 @@ t.string "mastodon" t.string "youtube" t.string "blog" + t.string "contactable_type" + t.integer "contactable_id" end create_table "delayed_jobs", force: :cascade do |t| diff --git a/spec/controllers/tracks_controller_spec.rb b/spec/controllers/tracks_controller_spec.rb index 3216e8d094..990365f9cb 100644 --- a/spec/controllers/tracks_controller_spec.rb +++ b/spec/controllers/tracks_controller_spec.rb @@ -8,6 +8,7 @@ let(:conference) { create(:conference) } let!(:regular_track) { create(:track, program: conference.program) } let!(:self_organized_track) { create(:track, :self_organized, program: conference.program, submitter: user, name: 'My awesome track', color: '#800080') } + let!(:contact) { create(:contact, contactable_type: 'Track', contactable_id: self_organized_track.id) } before :each do sign_in(user)