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

Add to_component_class to active record base #129

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/action_view/component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "action_view/component/monkey_patch"
require "action_view/component/ar_monkey_patch"
require "action_view/component/base"
require "action_view/component/railtie"
9 changes: 9 additions & 0 deletions lib/action_view/component/ar_monkey_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class ActiveRecord::Base
vinistock marked this conversation as resolved.
Show resolved Hide resolved
def to_component_class
class_name = "#{self.class.name}Component"

class_name.constantize if defined?(class_name)
vinistock marked this conversation as resolved.
Show resolved Hide resolved
end
end
12 changes: 2 additions & 10 deletions lib/action_view/component/monkey_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def render(options = {}, args = {}, &block)
options.render_in(self, &block)
elsif options.is_a?(Class) && options < ActionView::Component::Base
options.new(args).render_in(self, &block)
elsif options.is_a?(Hash) && options.has_key?(:component)
options[:component].new(options[:locals]).render_in(self, &block)
elsif options.respond_to?(:to_component_class) && !options.to_component_class.nil?
vinistock marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what case does the nil? check help us here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are adding the to_component_class to every model, we need to check that the model does indeed have a corresponding component class before trying to render it.

It will return nil if the component class doesn't exist for the model.

component_class = options.to_component_class
initialize_params = component_class.instance_method(:initialize).parameters.map(&:last)
component_attributes = options.attributes.symbolize_keys.slice(*initialize_params)

component_class.new(component_attributes).render_in(self, &block)
elsif options.is_a?(Hash) && options.has_key?(:component)
options[:component].new(options[:locals]).render_in(self, &block)
else
super
end
Expand All @@ -31,11 +31,3 @@ def render(options = {}, args = {}, &block)

prepend RenderMonkeyPatch
end

class ActiveRecord::Base
def to_component_class
class_name = "#{self.class.name}Component"

class_name.constantize if defined?(class_name)
end
end