Refactor moments_form_helper.rb#2362
Conversation
Refactored code to adhere to single responsibility principle a bit better, form input is encapsulated in a class now. Should also make things a bit easier to read/maintain.
Corrected form input so that switch input functions as intended, had to make light controller modifications.
|
Thank you for opening this pull request with us! Be sure to follow our Pull Request Practices. Let us know if you have any questions on Slack. |
|
I'm seeing there were test failures introduced here. I would look into this! Let me know if you need help! |
| def create | ||
| @moment = current_user.moments.build(moment_params) | ||
| @moment.published_at = Time.zone.now if publishing? | ||
| @moment.published_at = params[:moment][:publishing] == '1' ? Time.zone.now : nil # Use unfiltered params |
There was a problem hiding this comment.
I'm curious why we're using params[:moment][:publishing] instead of publishing??
There was a problem hiding this comment.
It's been a while, but iirc I made the change because I was struggling to get the save as draft switch to function correctly, as it was essentially either always a draft or always publishing otherwise. I just went back for a closer look since our tests didn't pass, and now it isn't functional so I must've failed to push something at some point. I'll be going back to see if I can get it working again when I get a chance tonight.
| def update | ||
| if (publishing? && !@moment.published?) || saving_as_draft? | ||
| @moment.published_at = !saving_as_draft? && Time.zone.now | ||
| if params[:moment][:publishing] |
There was a problem hiding this comment.
What about saving_as_draft??
switch behavior adjustments in the moments_form_helper
|
Hey guys,i'm looking for a place to contribute and ended here. Great work on the refactor! This definitely improves the code structure and makes the helper easier to maintain. I have three suggestions to finalize this:
# spec/forms/form_input_spec.rb
require 'rails_helper'
RSpec.describe FormInput, type: :model do
it 'builds with basic attributes' do
input = FormInput.new(id: 'test', type: 'text', name: 'form[test]', label: 'Test')
expect(input.to_h).to include(id: 'test', type: 'text', name: 'form[test]', label: 'Test')
end
it 'returns a new input with additional attributes' do
input = FormInput.new(id: 'test', type: 'text', name: 'form[test]', label: 'Test')
updated_input = input.with_attributes(placeholder: 'Enter text')
expect(updated_input.to_h[:placeholder]).to eq('Enter text')
end
it 'provides an empty input' do
empty_input = FormInput.empty
expect(empty_input.to_h.values).to all(be_nil)
end
end
Example of what needs to be added: Before (old expected input): {
id: 'moment_comment',
type: 'switch',
name: 'moment[comment]',
label: 'Allow Comments?',
value: true
}Now (new structure): {
id: 'moment_comment_hidden',
type: 'hidden',
name: 'moment[comment]',
value: '0'
},
{
id: 'moment_comment',
type: 'switch',
name: 'moment[comment]',
label: 'Allow Comments?',
unchecked_value: '1',
value: '0',
dark: true,
info: 'Only you and viewers can comment'
}Once these adjustments are made, the helper specs should pass. Hope this helps. |
|
Closing this PR and helping to take this to the finish line here: #2431 |
Description
This pull request aims to improve the architecture of the current codebase. The class
moments_form_helper.rbviolates the Single Responsibility Principle because it handles multiple responsibilities.Key Changes
FormInputwill handle the input properties of the moments form.MomentsFormHelper, methods includingmoments_input_props,moment_publishing,moment_bookmarked, andmoment_display_resourceswill be updated to create a newFormInputwith all the information instead of formatting the input within the methods.More Details
This is important because:
MomentsFormHelpernow only handles business logic and not form input construction.FormInputobject can be used in other classes as well, making the codebase more extendable.Corresponding Issue
#2350
Reviewing this pull request? Check out our Code Review Practices guide if you haven't already!