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

Fix HTML options recursion #349

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,41 @@
name: Test

on: push

jobs:
test:
name: Run tests

runs-on: ubuntu-latest
env:
RAILS_ENV: test

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
bundler: 1.17.2
bundler-cache: true

- name: Setup Node.js environment
uses: actions/setup-node@v3.5.1
with:
node-version: '14.x'

- name: Install JS packages
run: npm install && npm install -g bower && bower install

- name: Add node modules to PATH
run: echo "./node_modules/.bin" >> $GITHUB_PATH

- name: Lint Ruby
run: bundle exec rubocop

- name: Lint JS
run: jscs assets/js

- name: Run tests
run: bundle exec rspec && npm test
2 changes: 1 addition & 1 deletion .rubocop.yml
@@ -1,4 +1,4 @@
inherit_from:
- https://raw.githubusercontent.com/moneyadviceservice/mas-standards/master/.rubocop.yml
- https://raw.githubusercontent.com/moneyadviceservice/mas-standards/heroku-migration/.rubocop.yml
- .rubocop_todo.yml
require: rubocop-rspec
33 changes: 21 additions & 12 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-02-05 15:09:24 +0000 using RuboCop version 0.63.1.
# on 2023-05-11 17:44:41 +0100 using RuboCop version 0.80.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -11,10 +11,17 @@ Lint/ImplicitStringConcatenation:
Exclude:
- 'spec/lib/dough/helpers/form_row_spec.rb'

# Offense count: 1
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
Naming/FileName:
Exclude:
- 'lib/dough-ruby.rb'

# Offense count: 1
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
# AllowedNames: io, id, to, by, on, in, at, ip, db
Naming/UncommunicativeMethodParamName:
# AllowedNames: io, id, to, by, on, in, at, ip, db, os, pp
Naming/MethodParameterName:
Exclude:
- 'lib/dough/html_options.rb'

Expand Down Expand Up @@ -44,6 +51,13 @@ RSpec/InstanceVariable:
Exclude:
- 'spec/lib/dough/helpers/form_row_spec.rb'

# Offense count: 3
RSpec/LeakyConstantDeclaration:
Exclude:
- 'spec/lib/dough/forms/builder_spec.rb'
- 'spec/lib/dough/forms/builders/validation_spec.rb'
- 'spec/lib/dough/helpers_spec.rb'

# Offense count: 5
# Configuration parameters: .
# SupportedStyles: have_received, receive
Expand Down Expand Up @@ -82,15 +96,9 @@ RSpec/VerifiedDoubles:
- 'spec/lib/dough/helpers_spec.rb'

# Offense count: 1
Rails/OutputSafety:
Exclude:
- 'lib/dough/helpers.rb'

# Offense count: 2
Style/MethodMissingSuper:
Exclude:
- 'lib/dough/helpers.rb'
- 'lib/dough/html_options.rb'

# Offense count: 2
Style/MissingRespondToMissing:
Expand All @@ -112,8 +120,9 @@ Style/OptionalArguments:
Exclude:
- 'lib/dough/forms/builders/validation.rb'

# Offense count: 48
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# Offense count: 45
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Layout/LineLength:
Max: 151
2 changes: 1 addition & 1 deletion .ruby-version
@@ -1 +1 @@
2.5.3
2.7.7
13 changes: 5 additions & 8 deletions Gemfile
@@ -1,25 +1,22 @@
source 'https://rubygems.org'
source 'http://gems.dev.mas.local' if ENV['MAS_BUILD']

gemspec

ruby '2.5.3'
gem 'rails', '5.0.7.2'
ruby IO.read('.ruby-version').chomp

gem 'rails', '~> 6.1'

group :development, :test do
gem 'brakeman', require: false
gem 'capybara', '2.4.1'
gem 'danger', require: false
gem 'danger-rubocop', require: false
gem 'mas-development_dependencies', github: 'moneyadviceservice/mas-development_dependencies', ref: 'de00ee4'
gem 'pry'
gem 'rspec-rails'
gem 'rubocop', '~> 0.63.1', require: false
gem 'rubocop', '0.80.0', require: false
gem 'rubocop-rspec'
gem 'sass'
gem 'shoulda-matchers', '~> 2.8.0' # Dough targets 1.9.3 so we need to lock shoulda-matchers to < 3.0
gem 'shoulda-matchers'
gem 'sprockets', '~> 3.7.2'
gem 'tzinfo-data'
end

gem 'mas-build' if ENV['MAS_BUILD']
Expand Up @@ -2,7 +2,7 @@
<div class="validation-summary__content-container">
<p class="validation-summary__title"><%= t('dough.forms.validation.summary.title') %></p>
<ol class="validation-summary__list" data-dough-validation-summary-list>
<%= render partial: 'summary_for_errors_li', collection: errors, as: 'error', locals: { error_prefix: error_prefix } %>
<%= render partial: 'dough/forms/builders/validation/summary_for_errors_li', collection: errors, as: 'error', locals: { error_prefix: error_prefix } %>
</ol>
</div>
</div>
85 changes: 85 additions & 0 deletions assets/js/components/PostMessages.js
@@ -0,0 +1,85 @@
/**
* This is a generic component designed to allow communication between
* MAS components and a host page on the Money Helper site
* It uses the postMessage method to send an object to the host environment
*/
define(['DoughBaseComponent'],
function(DoughBaseComponent) {
'use strict';

var PostMessages,
defaultConfig = {},
message = {
jumpLink: {
id: '',
offset: 0
}
};

PostMessages = function($el, config) {
PostMessages.baseConstructor.call(this, $el, config, defaultConfig);

this.message = message;
};

/**
* Inherit from base module, for shared methods and interface
*/
DoughBaseComponent.extend(PostMessages);

PostMessages.componentName = 'PostMessages';

/**
* Adds listeners for click events to jump links
*/
PostMessages.prototype._addEvents = function() {
var _this = this;
var anchors = this.$el.find('a');

for (var anchor in anchors) {
if (anchors[anchor].href && anchors[anchor].href.indexOf('#') > -1) {
$(anchors[anchor]).on('click', function(e) {
e.preventDefault();
_this._updateMessage(e.target.href.split('#')[1]);
})
}
};
}

/**
* Updates the message with vertical offset value for the supplied element
*/
PostMessages.prototype._updateMessage = function(id) {
var offset = this._getOffset(id);
this.message.jumpLink.id = id;
this.message.jumpLink.offset = offset;

this._sendMessage();
}

/**
* Gets the vertical offset value of the required element
*/
PostMessages.prototype._getOffset = function(id) {
var el = this.$el.find('#' + id);

return el[0].getBoundingClientRect().top;
}

/**
* Sends the message
*/
PostMessages.prototype._sendMessage = function() {
window.parent.postMessage(this.message, '*');
}

/**
* @param {Promise} initialised
*/
PostMessages.prototype.init = function(initialised) {
this._initialisedSuccess(initialised);
this._addEvents();
};

return PostMessages;
});
20 changes: 6 additions & 14 deletions lib/dough/forms/builder.rb
Expand Up @@ -14,7 +14,7 @@ class Builder < ActionView::Helpers::FormBuilder
# <%= f.errors_summary %>
#
def errors_summary
render(errors_summary_partial_name, errors: object_errors) if object_errors.present?
ApplicationController.render(partial: errors_summary_partial_name, locals: { errors: object_errors }) if object_errors.present?
end

# This is the partial used to render the summary errors.
Expand All @@ -28,7 +28,7 @@ def errors_summary
# end
#
def errors_summary_partial_name
'errors_summary'
'dough/forms/builder/errors_summary'
end

# Returns *all* error messages for the field passed on the argument.
Expand All @@ -40,7 +40,7 @@ def errors_summary_partial_name
#
def errors_for(field_name)
errors = object_errors.select { |error, _| error.field_name == field_name }
render(partial: errors_for_partial_name, collection: errors, as: 'error')
ApplicationController.render(partial: errors_for_partial_name, collection: errors, as: 'error')
end

# This is the partial used to render the summary errors.
Expand All @@ -54,15 +54,15 @@ def errors_for(field_name)
# end
#
def errors_for_partial_name
'errors_for'
'dough/forms/builder/errors_for'
end

def object_errors
object.errors.map.each_with_index do |error, index|
object_error_class.new(
object: object,
field_name: error[0],
message: error[1],
field_name: error.attribute,
message: error.message,
counter: index + 1,
prefix: object_name
)
Expand Down Expand Up @@ -103,14 +103,6 @@ def object_error_class
def partial_paths
[Dough::Engine.root.join('app/views/dough/forms/builder/')]
end

def lookup_context
ActionView::LookupContext.new(ActionController::Base.view_paths + partial_paths)
end

def view_renderer
ActionView::Renderer.new(lookup_context)
end
end
end
end
24 changes: 11 additions & 13 deletions lib/dough/forms/builders/validation.rb
Expand Up @@ -10,14 +10,22 @@ module ValidationModule
include ActionView::Helpers::TranslationHelper

def validation_summary
render 'summary_for_errors', errors: errors, error_prefix: error_prefix
ApplicationController.render(
partial: 'dough/forms/builders/validation/summary_for_errors',
locals: { errors: errors, error_prefix: error_prefix }
)
end

def errors_for(subject = nil, field)
subject ||= object
filtered_errors = errors.select { |hash| hash[:object] == subject && hash[:field] == field }

render partial: 'errors_for_field', collection: filtered_errors, as: 'error', locals: { error_prefix: error_prefix }
ApplicationController.render(
partial: 'dough/forms/builders/validation/errors_for_field',
collection: filtered_errors,
as: 'error',
locals: { error_prefix: error_prefix }
)
end

def validates(*models)
Expand All @@ -28,16 +36,6 @@ def error_count
errors.count
end

def lookup_context
ActionView::LookupContext.new(
ActionController::Base.view_paths + [Dough::Engine.root.join('app/views/dough/forms/builders/validation')]
)
end

def view_renderer
ActionView::Renderer.new(lookup_context)
end

private

def error_models
Expand All @@ -63,7 +61,7 @@ def collate_model_errors(model)
field_order = Array(model.try(:field_order))

[].tap do |model_errors|
(field_order | model.errors.keys).each do |field|
(field_order | model.errors.attribute_names).each do |field|
model.errors.full_messages_for(field).each do |message|
model_errors << [field, message]
end
Expand Down
10 changes: 5 additions & 5 deletions lib/dough/html_options.rb
Expand Up @@ -7,12 +7,12 @@ class HtmlOptions < OpenStruct
def method_missing(m, *args, &block)
name = m.to_sym

unless respond_to?(name)
new_ostruct_member(name)
public_send("#{name.to_sym}=", '')
end
self[name] = '' unless respond_to?(name)
super
end

super(m, *args, &block)
def classes
self[:classes] ||= ''
end

def to_s
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -35,15 +35,14 @@
"karma": "^1.7.0",
"karma-chai": "^0.1.0",
"karma-chai-jquery": "^1.0.0",
"karma-chrome-launcher": "^0.1.2",
"karma-chrome-launcher": "^3.2.0",
"karma-cli": "^1.0.1",
"karma-coverage": "^0.2.4",
"karma-fixture": "^0.2.6",
"karma-html2js-preprocessor": "^1.1.0",
"karma-jquery": "^0.2.2",
"karma-mocha": "^1.3.0",
"karma-osx-reporter": "*",
"karma-phantomjs-launcher": "^1.0.4",
"karma-requirejs": "1.1.0",
"karma-sinon": "^1.0.3",
"karma-spec-reporter": "0.0.32",
Expand Down