Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails5 ActiveRecord monkey patch for SchemaDumper #2571

Merged
merged 9 commits into from
Jun 24, 2020
2 changes: 1 addition & 1 deletion .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

# Extract the Ruby version from the Gemfile.lock
- name: 'Determine Ruby Version'
run: echo ::set-env name=RUBY_VERSION::$(echo `cat ./Gemfile.lock | grep -A 1 'RUBY VERSION' | grep 'ruby' | grep -oE '[0-9]\.[0-9]'`)
run: echo ::set-env name=RUBY_VERSION::$(echo `cat ./Gemfile.lock | grep -A 1 'RUBY VERSION' | grep 'ruby' | grep -oE '[0-9]\.[0-9]'`)

# Install Ruby - using the version found in the Gemfile.lock
- name: 'Install Ruby'
Expand Down
14 changes: 8 additions & 6 deletions app/controllers/org_admin/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def show
section: { phase: :template })
.find(params[:id])
authorize question
render partial: "show", locals: {
render json: { html: render_to_string(partial: "show", locals: {
template: question.section.phase.template,
section: question.section,
question: question,
conditions: question.conditions
}
})}
end

def open_conditions
Expand All @@ -37,19 +37,21 @@ def open_conditions
webhooks: webhook_hash(question.conditions) }
end


# GET /org_admin/templates/[:template_id]/phases/[:phase_id]/sections/[:id]/questions/[:question_id]/edit
def edit
question = Question.includes(:annotations,
:question_options,
section: { phase: :template })
.find(params[:id])
authorize question
render partial: "edit", locals: {
render json: { html: render_to_string(partial: "edit", locals: {
template: question.section.phase.template,
section: question.section,
question: question,
question_formats: allowed_question_formats,
conditions: question.conditions
}
})}
end

def new
Expand All @@ -61,7 +63,7 @@ def new
number: nbr.present? ? nbr + 1 : 1)
question_formats = allowed_question_formats
authorize question
render partial: "form", locals: {
render json: { html: render_to_string(partial: "form", locals: {
template: section.phase.template,
section: section,
question: question,
Expand All @@ -71,7 +73,7 @@ def new
phase_id: section.phase.id,
id: section.id),
question_formats: question_formats
}
}) }
end

def create
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/org_admin/sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def edit
else
"show"
end
render partial: partial_name,
render json: { html: render_to_string(partial: partial_name,
locals: {
template: section.phase.template,
phase: section.phase,
section: section
}
})}
end

# POST /org_admin/templates/[:template_id]/phases/[:phase_id]/sections
Expand Down
14 changes: 7 additions & 7 deletions app/javascript/src/orgAdmin/conditions/updateConditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,27 @@ export default function updateConditions(id) {
}
};

// display conditions (editing) upon click of 'Add Logic'
parent.on('ajax:success', 'a.add-logic[data-remote="true"]', (e, data) => {
// display conditions (editing) upon click of 'Add Conditions'
parent.on('ajax:success', 'a.add-logic[data-remote="true"]', (e) => {
addLogicButton.attr('data-loaded', 'true');
addLogicButton.addClass('disabled');
addLogicButton.blur();
addLogicButton.text('Conditions');
if (isObject(content)) {
content.html(data.container);
content.html(e.detail[0].container);
}
setSelectPicker();
webhookForm(data.webhooks, undefined);
webhookForm(e.detail[0].webhooks, undefined);
});

// add condition
parent.on('ajax:success', 'a.add-condition[data-remote="true"]', (e, data) => {
parent.on('ajax:success', 'a.add-condition[data-remote="true"]', (e) => {
const conditionList = $(e.target).closest('#condition-container').find('.condition-list');
const addDiv = $(e.target).closest('#condition-container').find('.add-condition-div');
if (isObject(conditionList)) {
conditionList.attr('data-loaded', 'true');
conditionList.append(data.attachment_partial);
addDiv.html(data.add_link);
conditionList.append(e.detail[0].attachment_partial);
addDiv.html(e.detail[0].add_link);
conditionList.attr('data-loaded', 'false');
setSelectPicker();
const selectObject = conditionList.find('.selectpicker.action-type').last();
Expand Down
8 changes: 4 additions & 4 deletions app/javascript/src/orgAdmin/phases/newEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ $(() => {
return panelBody.attr('data-loaded') === 'false';
});

$(parentSelector).on('ajax:success', 'a.ajaxified-section[data-remote="true"]', (e, data) => {
$(parentSelector).on('ajax:success', 'a.ajaxified-section[data-remote="true"]', (e) => {
const panelBody = $(e.target).parent().find('.panel-body');
const panel = panelBody.parent();
if (isObject(panelBody)) {
// Display the section's html
panelBody.attr('data-loaded', 'true');
panelBody.html(data);
panelBody.html(e.detail[0].html);
// Wire up the section
initSection(`#${panel.attr('id')}`);
}
Expand All @@ -123,13 +123,13 @@ $(() => {
});
}
});
$(parentSelector).on('ajax:success', 'a.ajaxified-question[data-remote="true"]', (e, data) => {
$(parentSelector).on('ajax:success', 'a.ajaxified-question[data-remote="true"]', (e) => {
const target = $(e.target);
const panelBody = getQuestionPanel(target);
if (isObject(panelBody)) {
const id = panelBody.attr('id');
// Display the section's html
panelBody.html(data);
panelBody.html(e.detail[0].html);
initQuestion(id);
updateConditions(id);
if (panelBody.is('.new-question')) {
Expand Down
19 changes: 10 additions & 9 deletions app/services/org/create_created_plan_service.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

#import statements fix Circular dependancy errors due to threading
import OrgDateRangeable
import StatCreatedPlan
import StatCreatedPlan::CreateOrUpdate
import Role
import User
import Plan
import Perm
import Template
# statements fix Circular dependancy errors due to threading
# see: https://github.com/grosser/parallel#nameerror-uninitialized-constant
OrgDateRangeable.class
StatCreatedPlan.class
StatCreatedPlan::CreateOrUpdate.class
Role.class
User.class
Plan.class
Perm.class
Template.class

class Org

Expand Down
17 changes: 9 additions & 8 deletions app/services/org/create_exported_plan_service.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

#import statements fix Circular dependancy errors
import OrgDateRangeable
import StatExportedPlan
import StatExportedPlan::CreateOrUpdate
import Role
import Plan
import User
import ExportedPlan
# statements fix Circular dependancy errors due to threading
# see: https://github.com/grosser/parallel#nameerror-uninitialized-constant
OrgDateRangeable.class
StatExportedPlan.class
StatExportedPlan::CreateOrUpdate.class
Role.class
Plan.class
User.class
ExportedPlan.class

class Org

Expand Down
11 changes: 6 additions & 5 deletions app/services/org/create_joined_user_service.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

#import statements fix Circular dependancy errors due to threading
import OrgDateRangeable
import StatJoinedUser
import StatJoinedUser::CreateOrUpdate
import User
# statements fix Circular dependancy errors due to threading
# see: https://github.com/grosser/parallel#nameerror-uninitialized-constant
OrgDateRangeable.class
StatJoinedUser.class
StatJoinedUser::CreateOrUpdate.class
User.class

class Org

Expand Down
19 changes: 10 additions & 9 deletions app/services/org/create_last_month_created_plan_service.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

#import statements fix Circular dependancy errors due to threading
import OrgDateRangeable
import StatCreatedPlan
import StatCreatedPlan::CreateOrUpdate
import Role
import User
import Plan
import Perm
import Template
# statements fix Circular dependancy errors due to threading
# see: https://github.com/grosser/parallel#nameerror-uninitialized-constant
OrgDateRangeable.class
StatCreatedPlan.class
StatCreatedPlan::CreateOrUpdate.class
Role.class
User.class
Plan.class
Perm.class
Template.class


class Org
Expand Down
17 changes: 9 additions & 8 deletions app/services/org/create_last_month_exported_plan_service.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

#import statements fix Circular dependancy errors
import OrgDateRangeable
import StatExportedPlan
import StatExportedPlan::CreateOrUpdate
import Role
import Plan
import User
import ExportedPlan
# statements fix Circular dependancy errors due to threading
# see: https://github.com/grosser/parallel#nameerror-uninitialized-constant
OrgDateRangeable.class
StatExportedPlan.class
StatExportedPlan::CreateOrUpdate.class
Role.class
Plan.class
User.class
ExportedPlan.class

class Org

Expand Down
11 changes: 6 additions & 5 deletions app/services/org/create_last_month_joined_user_service.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

#import statements fix Circular dependancy errors due to threading
import OrgDateRangeable
import StatJoinedUser
import StatJoinedUser::CreateOrUpdate
import User
# statements fix Circular dependancy errors due to threading
# see: https://github.com/grosser/parallel#nameerror-uninitialized-constant
OrgDateRangeable.class
StatJoinedUser.class
StatJoinedUser::CreateOrUpdate.class
User.class

class Org

Expand Down
15 changes: 8 additions & 7 deletions app/services/org/create_last_month_shared_plan_service.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

#import statements fix Circular dependancy errors due to threading
import OrgDateRangeable
import StatSharedPlan
import StatSharedPlan::CreateOrUpdate
import User
import Plan
import Role
# statements fix Circular dependancy errors due to threading
# see: https://github.com/grosser/parallel#nameerror-uninitialized-constant
OrgDateRangeable.class
StatSharedPlan.class
StatSharedPlan::CreateOrUpdate.class
User.class
Plan.class
Role.class

class Org

Expand Down
15 changes: 8 additions & 7 deletions app/services/org/create_shared_plan_service.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

#import statements fix Circular dependancy errors due to threading
import OrgDateRangeable
import StatSharedPlan
import StatSharedPlan::CreateOrUpdate
import User
import Plan
import Role
# statements fix Circular dependancy errors due to threading
# see: https://github.com/grosser/parallel#nameerror-uninitialized-constant
OrgDateRangeable.class
StatSharedPlan.class
StatSharedPlan::CreateOrUpdate.class
User.class
Plan.class
Role.class

class Org

Expand Down
71 changes: 71 additions & 0 deletions config/initializers/dmproadmap_schema_dumper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

module ActiveRecord

# The Rails 5.x SchemaDumper includes an `options:` arrgument on table definitions
# that is not DB agnostic. This Monkey Patch comments out the `options:` section.
#
# TODO: Determine if this is still necessary in Rails 6.x+
class SchemaDumper

# Method definition taken from the 5.2-stable branch of ActiveRecord:
# https://github.com/rails/rails/blob/5-2-stable/activerecord/lib/active_record/schema_dumper.rb
def table(table, stream)
columns = @connection.columns(table)
begin
tbl = StringIO.new

# first dump primary key column
pk = @connection.primary_key(table)

tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}"

case pk
when String
tbl.print ", primary_key: #{pk.inspect}" unless pk == "id"
pkcol = columns.detect { |c| c.name == pk }
pkcolspec = column_spec_for_primary_key(pkcol)
if pkcolspec.present?
tbl.print ", #{format_colspec(pkcolspec)}"
end
when Array
tbl.print ", primary_key: #{pk.inspect}"
else
tbl.print ", id: false"
end

# Commenting out Table Options because they are not DB agnostic
# table_options = @connection.table_options(table)
# if table_options.present?
# tbl.print ", #{format_options(table_options)}"
# end

tbl.puts ", force: :cascade do |t|"

# then dump all non-primary key columns
columns.each do |column|
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type)
next if column.name == pk
type, colspec = column_spec(column)
tbl.print " t.#{type} #{column.name.inspect}"
tbl.print ", #{format_colspec(colspec)}" if colspec.present?
tbl.puts
end

indexes_in_create(table, tbl)

tbl.puts " end"
tbl.puts

tbl.rewind
stream.print tbl.read
rescue => e
stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
stream.puts "# #{e.message}"
stream.puts
end
end

end

end