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 all commits
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 .github/workflows/ruby_on_rails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
ruby-version: ${{ matrix.ruby_version }}
- name: Build and test with Rake
run: |
sudo apt-get install libsqlite3-dev
gem install bundler:1.14.0
bundle update
bundle install --jobs 4 --retry 3
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ build-iPhoneSimulator/
# .ruby-version
# .ruby-gemset

/**/*.sqlite3

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gemspec
rails_version = "#{ENV['RAILS_VERSION'] || '6.0.0'}"

gem "rails", rails_version == "master" ? { github: "rails/rails" } : rails_version
gem "sqlite3", rails_version >= "6.0.0" ? ">= 1.4.0" : "< 1.4.0"
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.4.1)
temple (0.8.1)
thor (0.20.3)
thread_safe (0.3.6)
Expand All @@ -171,6 +172,7 @@ DEPENDENCIES
rubocop (= 0.74)
rubocop-github (~> 0.13.0)
slim (~> 4.0)
sqlite3 (>= 1.4.0)

BUNDLED WITH
1.17.3
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ Components can be rendered via:

`render(component: TestComponent, locals: { foo: :bar })`

**Rendering components through models**

Passing model instances will cause `render` to look for its respective component class.

The component is instantiated with the rendered model instance.

Example for a `Post` model:

`render(@post)`

```ruby
class PostComponent < ActionView::Component
def initialize(post)
@post = post
end
end
```

The following syntax has been deprecated and will be removed in v2.0.0:

`render(TestComponent.new(foo: :bar))`
Expand Down
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/active_model_conversion_monkey_patch"
require "action_view/component/base"
require "action_view/component/railtie"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module ActiveModel
module Conversion
def to_component_class
"#{self.class.name}Component".safe_constantize
end
end
end
2 changes: 2 additions & 0 deletions lib/action_view/component/monkey_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def render(options = {}, args = {}, &block)
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?
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.

options.to_component_class.new(options).render_in(self, &block)
else
super
end
Expand Down
7 changes: 7 additions & 0 deletions test/action_view/component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ def test_renders_component_with_rb_in_its_name
assert_equal "Editorb!\n", render_inline(EditorbComponent).text
end

def test_to_component_class
post = Post.new(title: "Awesome post")

assert_equal PostComponent, post.to_component_class
assert_equal "<span>The Awesome post component!</span>", render_inline(post).first.to_html
end

private

def modify_file(file, content)
Expand Down
1 change: 1 addition & 0 deletions test/app/components/post_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span>The <%= @post.title %> component!</span>
7 changes: 7 additions & 0 deletions test/app/components/post_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class PostComponent < ActionView::Component::Base
def initialize(post)
@post = post
end
end
5 changes: 5 additions & 0 deletions test/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
4 changes: 4 additions & 0 deletions test/app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Post < ApplicationRecord
end
1 change: 1 addition & 0 deletions test/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require File.expand_path("../boot", __FILE__)

require "active_record/railtie"
require "active_model/railtie"
require "action_controller/railtie"
require "action_view/railtie"
Expand Down
8 changes: 8 additions & 0 deletions test/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

test:
<<: *default
database: db/test.sqlite3
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@
def trim_result(html)
html.delete(" \t\r\n")
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title
end
end