Skip to content

Commit

Permalink
Expand CI Matrix
Browse files Browse the repository at this point in the history
Support Rails 7.1 and 7.2. Experiment with Ruby 3.3.
  • Loading branch information
seanpdoyle committed Sep 28, 2024
1 parent 50a2438 commit fc65260
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 40 deletions.
22 changes: 16 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ["2.7", "3.0", "3.1", "3.2"]
rails: ["6.0", "6.1", "7.0"]
ruby:
- "2.7"
- "3.0"
- "3.1"
- "3.2"
- "3.3"
rails:
- "6.1"
- "7.0"
- "7.1"
- "7.2"
include:
- ruby: "2.7"
rails: "5.2"
- ruby: "3.2"
rails: "main"
- { ruby: "3.2", rails: "main" }
- { ruby: "3.3", rails: "main" }
exclude:
- { ruby: "2.7", rails: "7.2" }
- { ruby: "3.0", rails: "7.2" }

env:
RAILS_VERSION: "${{ matrix.rails }}"
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ version links.

## main

Expand matrix of supported versions to include `[email protected]` and `[email protected]`.

## [0.2.1] - Jan 22, 2023

Add test coverage for `field_id` and `field_name`, and delegate to view context
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ gemspec
# To use a debugger
# gem 'byebug', group: [:development, :test]

rails_version = ENV.fetch("RAILS_VERSION", "7.0")
rails_version = ENV.fetch("RAILS_VERSION", "7.2")

rails_constraint =
if rails_version == "main"
Expand All @@ -26,8 +26,8 @@ rails_constraint =
gem "rails", rails_constraint
gem "activemodel", rails_constraint
gem "sprockets-rails"
gem "sqlite3", (rails_version == "main") ? ">= 2.1" : "~> 1.4"

group :test do
gem "activerecord-nulldb-adapter"
gem "minitest-around"
end
10 changes: 10 additions & 0 deletions lib/view_partial_form_builder/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,15 @@ class Engine < ::Rails::Engine
ActiveSupport.on_load(:action_controller_base) do
default_form_builder ViewPartialFormBuilder::FormBuilder
end

ActiveSupport.on_load(:view_partial_form_builder) do
if Rails::VERSION::MAJOR > 7
self.aliased_field_helpers = {
checkbox: [:check_box],
collection_checkboxes: [:collection_check_boxes],
rich_textarea: [:rich_text_area]
}
end
end
end
end
4 changes: 4 additions & 0 deletions lib/view_partial_form_builder/form_builder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module ViewPartialFormBuilder
class FormBuilder < ActionView::Helpers::FormBuilder
class_attribute :aliased_field_helpers, default: {}

attr_reader :default

def initialize(*)
Expand All @@ -8,4 +10,6 @@ def initialize(*)
@template = TemplateProxy.new(builder: self, template: @template)
end
end

ActiveSupport.run_load_hooks(:view_partial_form_builder, FormBuilder)
end
38 changes: 24 additions & 14 deletions lib/view_partial_form_builder/template_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ViewPartialFormBuilder
class TemplateProxy
delegate :_object_for_form_builder, :field_id, :field_name, to: :@template
delegate :field_id, :field_name, to: :@template

def initialize(builder:, template:)
@template = template
Expand Down Expand Up @@ -35,19 +35,21 @@ def label(object_name, method, content_or_options = nil, options = nil, &block)
private

def method_missing(name, *arguments, &block)
arguments_after_object_name = arguments.from(1)
if @builder.respond_to?(name)
arguments_after_object_name = arguments.from(1)

render(name, arguments: arguments_after_object_name, block: block) do
if @template.respond_to?(name)
render(name, arguments: arguments_after_object_name, block: block) do
@template.public_send(name, *arguments, &block)
else
super
end
elsif @template.respond_to?(name)
@template.public_send(name, *arguments, &block)
else
super
end
end

def respond_to_missing?(name, include_private = false)
@template.respond_to_missing?(name, include_private)
@builder.respond_to_missing?(name) || @template.respond_to_missing?(name)
end

def render(partial_name, arguments:, block: nil, &fallback)
Expand Down Expand Up @@ -79,12 +81,20 @@ def find_partial(template_name, locals)
current_prefix = current_virtual_path.delete_suffix("/_#{template_name}")
template_is_partial = true

@template.lookup_context.find_all(
template_name,
lookup_override.prefixes_after(current_prefix),
template_is_partial,
locals.keys
).first
partials = template_names_for(template_name).lazy.map do |name|
@template.lookup_context.find_all(
name,
lookup_override.prefixes_after(current_prefix),
template_is_partial,
locals.keys
).first
end

partials.find(&:present?)
end

def template_names_for(template_name)
[template_name, *@builder.aliased_field_helpers[template_name]].compact
end

def about_to_recurse_infinitely?(partial)
Expand All @@ -102,7 +112,7 @@ def current_virtual_path
def lookup_override
LookupOverride.new(
prefixes: @template.lookup_context.prefixes,
object_name: @builder.object&.model_name || @builder.object_name,
object_name: @builder.object.try(:model_name) || @builder.object_name,
view_partial_directory: ViewPartialFormBuilder.view_partial_directory
)
end
Expand Down
10 changes: 0 additions & 10 deletions test/dummy/config/database.yml

This file was deleted.

1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Configure Rails Environment
ENV["RAILS_ENV"] = "test"
ENV["DATABASE_URL"] = "sqlite3::memory:"

require_relative "../test/dummy/config/environment"
ActiveRecord::Migrator.migrations_paths = [File.expand_path("../test/dummy/db/migrate", __dir__)]
Expand Down
15 changes: 7 additions & 8 deletions test/view_partial_form_builder/rich_text_area_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ def setup
end

test "renders a field-specific template" do
declare_template "application/_form.html.erb", <<~HTML
<%= form_with(model: Post.new) do |form| %>
<%= form.rich_text_area(:avatar) %>
<% end %>
HTML
declare_template "application/form_builder/_rich_text_area.html.erb", <<~HTML
declare_template "application/form_builder/_rich_textarea.html.erb", <<~HTML
<div class="wrapper">
<%= form.rich_text_area(method, options) %>
<%= form.rich_textarea(method, options) %>
</div>
HTML

render(partial: "application/form")
render inline: <<~ERB
<%= form_with(model: Post.new) do |form| %>
<%= form.rich_textarea(:avatar) %>
<% end %>
ERB

assert_select(".wrapper trix-editor")
end
Expand Down
55 changes: 55 additions & 0 deletions test/view_partial_form_builder/template_proxy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require "test_helper"

class ViewPartialFormBuilder::TemplateProxyTest < ActionView::TestCase
test "#capture is delegated to the template" do
render_with_template_proxy <<~ERB
<% content = template_proxy.capture do %>
Hello world
<% end %>
<%= content %>
ERB

assert_equal rendered.strip, "Hello world"
end

if Rails.version >= "7.0.0"
test "#_object_for_form_builder is delegated to the template" do
model_class = Class.new do
def to_s
"Hello from the model"
end
end

render_with_template_proxy <<~ERB, model: model_class.new
<%= template_proxy._object_for_form_builder(model) %>
ERB

assert_equal rendered.strip, "Hello from the model"
end

test "#field_id is delegated to the template" do
render_with_template_proxy <<~ERB
<%= template_proxy.field_id(:object_name, :method_name) %>
ERB

assert_equal rendered.strip, "object_name_method_name"
end

test "#field_name is delegated to the template" do
render_with_template_proxy <<~ERB
<%= template_proxy.field_name(:object_name, :method_name) %>
ERB

assert_equal rendered.strip, "object_name[method_name]"
end
end

def render_with_template_proxy(template, **locals)
render inline: <<~ERB, locals: {template: template, locals: locals}
<% template_proxy = ViewPartialFormBuilder::TemplateProxy.new(builder: nil, template: self) %>
<%= render inline: template, locals: {template_proxy: template_proxy, **locals} %>
ERB
end
end

0 comments on commit fc65260

Please sign in to comment.