Skip to content

fix validation on parent for Rails 5 #196

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

Closed
wants to merge 1 commit into from

Conversation

pacMakaveli
Copy link

This ( temporarily ) fixes the issue for Rails 5. I'm running out of time and I don't have time to test for Rails 4 right now, but will do tomorrow. I will also find a way to support both in case this fails on Rails 4 ( maybe because required is an unknown key for belongs_to on Rails 4 )

@pacMakaveli
Copy link
Author

Also, this fixes #195

@mceachen
Copy link
Collaborator

mceachen commented Jan 26, 2016 via email

DouglasUrner added a commit to DouglasUrner/closure_tree that referenced this pull request Feb 21, 2016
@jayfredlund
Copy link
Contributor

@mcheachen -- not sure if you have a preferred option here, but I'm building something out so I can continue using this with a new project of mine, and was thinking I could build it in a way that you'd potentially use:

Options (in no particular order of preference):

belongs_to_options = { class_name: _ct.model_class.to_s,
                       foreign_key: _ct.parent_column_name,
                       inverse_of: :children,
                       touch: _ct.options[:touch],
                       optional: true }
begin
  belongs_to :parent, belongs_to_options
rescue ArgumentError => e
  belongs_to :parent, belongs_to_options.except(:optional)
end
belongs_to_options = { class_name: _ct.model_class.to_s,
                       foreign_key: _ct.parent_column_name,
                       inverse_of: :children,
                       touch: _ct.options[:touch] }

if ActiveRecord::Associations::Builder::BelongsTo.valid_options([]).include?(:optional)
  belongs_to_options.merge({optional: true})
end

belongs_to :parent, belongs_to_options
belongs_to_options = { class_name: _ct.model_class.to_s,
                       foreign_key: _ct.parent_column_name,
                       inverse_of: :children,
                       touch: _ct.options[:touch] }

if Rails.version.to_i >= 5
  belongs_to_options.merge({optional: true})
end

belongs_to :parent, belongs_to_options

Similar to what you've done in other examples with the has_many ordering, it'd probably be good to do something similar and move all of the conditional logic into the support file:

# model.rb
belongs_to :parent, belongs_to_opts({ class_name: _ct.model_class.to_s,
                                      foreign_key: _ct.parent_column_name,
                                      inverse_of: :children,
                                      touch: _ct.options[:touch] })

# support.rb
def belongs_to_opts(opts)
   # add the "optional" key and logic checks
end 

Any thoughts?

@mceachen
Copy link
Collaborator

mceachen commented Feb 27, 2016 via email

@jayfredlund
Copy link
Contributor

@mceachen I've created PR #204 to handle this. I decided to simply be consistent with the existing support methods -- that way, reading the belongs_to in the model.rb code shows everything being added, and the support method will just rip out the optional tag if the ActiveRecord version is < 5 (belongs_to being optional is default behavior in versions prior to 5).

The edge tests are obviously failing on Travis still, since the version of rspec-rails we're using does not support ActiveRecord > 4.3.

@seuros seuros closed this Jul 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants