Skip to content

Commit

Permalink
Add contactable to tracks
Browse files Browse the repository at this point in the history
Added social media fields to track submission form
by using a polymorphic association with the
Contact Model.
  • Loading branch information
rishabhptr committed Jul 21, 2019
1 parent c5ad952 commit 1f170db
Show file tree
Hide file tree
Showing 7 changed files with 387 additions and 359 deletions.
9 changes: 7 additions & 2 deletions app/controllers/tracks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions app/models/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
11 changes: 11 additions & 0 deletions app/views/tracks/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
6 changes: 6 additions & 0 deletions db/migrate/20190612161235_add_polymorphic_to_contact.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 1f170db

Please sign in to comment.