Skip to content

Commit

Permalink
Event form: change code that updates CoC URL to fetch it over ajax
Browse files Browse the repository at this point in the history
Also fixes a bug where the CoC URL would be inaccurately set back
to the default URL when you 'save draft' or the like.
  • Loading branch information
tjgrathwell committed May 30, 2016
1 parent fa00cb5 commit 5448461
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/controllers/chapters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def destroy
redirect_to chapters_url
end

def code_of_conduct_url
skip_authorization
render text: @chapter.code_of_conduct_url
end

private

def chapter_params
Expand Down
4 changes: 4 additions & 0 deletions app/models/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ def has_leader?(user)
def destroyable?
(events_count + external_events_count) == 0
end

def code_of_conduct_url
organization.code_of_conduct_url || Event::DEFAULT_CODE_OF_CONDUCT_URL
end
end
4 changes: 4 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ def levels
course[:levels]
end

def code_of_conduct_url
chapter.try(:code_of_conduct_url) || DEFAULT_CODE_OF_CONDUCT_URL
end

private

DEFAULT_DETAIL_FILES = Dir[Rails.root.join('app', 'models', 'event_details', '*.html')]
Expand Down
8 changes: 4 additions & 4 deletions app/views/events/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@
<div class="field">
<script>
window.whenReady(function () {
var chapterOrganization = <%= Chapter.all.each_with_object({}) { |c, hsh| hsh[c.id] = c.organization_id }.to_json.html_safe %>;
var organizationCoc = <%= Organization.where.not(code_of_conduct_url: nil).each_with_object({}) { |o, hsh| hsh[o.id] = o.code_of_conduct_url }.to_json.html_safe %>;
$('#event_chapter_id').on('change', function () {
var chapterId = parseInt(this.value, 10);
$('#coc_url').attr('href', organizationCoc[chapterOrganization[chapterId]] || '<%= Event::DEFAULT_CODE_OF_CONDUCT_URL %>');
$('#coc').attr('checked', false);
$.get("/chapters/" + chapterId + "/code_of_conduct_url").then(function (codeOfConductUrl) {
$('#coc_url').attr('href', codeOfConductUrl);
});
});
});
</script>
Expand All @@ -279,7 +279,7 @@
'toggle-target' => '.coc-required',
'toggle-enable-when-checked' => true
} %>
I accept the <%= link_to 'Code of Conduct', Event::DEFAULT_CODE_OF_CONDUCT_URL, target: :blank, id: 'coc_url' %> and will communicate it at the beginning of the event.
I accept the <%= link_to 'Code of Conduct', @event.code_of_conduct_url, target: :blank, id: 'coc_url' %> and will communicate it at the beginning of the event.
</strong>
<% end %>
</div>
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
resources :leaders, only: [:index, :create, :destroy], controller: 'chapters/leaders' do
get :potential, on: :collection
end

member do
get :code_of_conduct_url
end
end

resources :regions do
Expand Down

0 comments on commit 5448461

Please sign in to comment.