Skip to content

Commit

Permalink
Remove OpenStruct from codebase (#2144)
Browse files Browse the repository at this point in the history
* Remove OpenStruct from codebase

* Update docs/CHANGELOG.md

---------

Co-authored-by: Joel Hawksley <joelhawksley@github.com>
  • Loading branch information
le0pard and joelhawksley authored Oct 28, 2024
1 parent 070f0dd commit 812acf9
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 24 deletions.
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ GEM
nokogiri (1.16.7)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
ostruct (0.6.0)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -355,7 +354,6 @@ DEPENDENCIES
net-imap
net-pop
net-smtp
ostruct
pry (~> 0.13)
puma (~> 6)
rails (~> 7.0.0)
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Remove OpenStruct from codebase.

*Oleksii Vasyliev*

## 3.19.0

* Relax Active Support version constraint in gemspec.
Expand Down
2 changes: 1 addition & 1 deletion test/sandbox/app/components/partial_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= render partial: "test_partial" %>
<%= render "test_partial" %>
<%= render OpenStruct.new(to_partial_path: "test_partial") %>
<%= render PartialModel.new(to_partial_path: "test_partial") %>
2 changes: 0 additions & 2 deletions test/sandbox/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# frozen_string_literal: true

require "ostruct"

class ApplicationController < ActionController::Base
end
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def controller_to_string_render_component
end

def products
@products = [OpenStruct.new(name: "Radio clock"), OpenStruct.new(name: "Mints")]
@products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")]
end

def inline_products
products = [OpenStruct.new(name: "Radio clock"), OpenStruct.new(name: "Mints")]
products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")]

render(ProductComponent.with_collection(products, notice: "Today only"))
end
Expand Down
1 change: 1 addition & 0 deletions test/sandbox/app/models/coupon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Coupon = Struct.new(:percent_off, keyword_init: true)
1 change: 1 addition & 0 deletions test/sandbox/app/models/partial_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PartialModel = Struct.new(:to_partial_path, keyword_init: true)
1 change: 1 addition & 0 deletions test/sandbox/app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Photo = Struct.new(:title, :caption, :url, keyword_init: true)
1 change: 1 addition & 0 deletions test/sandbox/app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Product = Struct.new(:name, keyword_init: true)
2 changes: 1 addition & 1 deletion test/sandbox/test/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def call
end

def setup
@products = [OpenStruct.new(name: "Radio clock"), OpenStruct.new(name: "Mints")]
@products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")]
@collection = ProductComponent.with_collection(@products, notice: "secondhand")
end

Expand Down
30 changes: 15 additions & 15 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def test_backtrace_returns_correct_file_and_line_number
end

def test_render_collection
products = [OpenStruct.new(name: "Radio clock"), OpenStruct.new(name: "Mints")]
products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")]
render_inline(ProductComponent.with_collection(products, notice: "On sale"))

assert_selector("h1", text: "Product", count: 2)
Expand All @@ -599,7 +599,7 @@ def test_render_collection
end

def test_render_collection_custom_collection_parameter_name
coupons = [OpenStruct.new(percent_off: 20), OpenStruct.new(percent_off: 50)]
coupons = [Coupon.new(percent_off: 20), Coupon.new(percent_off: 50)]
render_inline(ProductCouponComponent.with_collection(coupons))

assert_selector("h3", text: "20%")
Expand All @@ -608,8 +608,8 @@ def test_render_collection_custom_collection_parameter_name

def test_render_collection_custom_collection_parameter_name_counter
photos = [
OpenStruct.new(title: "Flowers", caption: "Yellow flowers", url: "https://example.com/flowers.jpg"),
OpenStruct.new(title: "Mountains", caption: "Mountains at sunset", url: "https://example.com/mountains.jpg")
Photo.new(title: "Flowers", caption: "Yellow flowers", url: "https://example.com/flowers.jpg"),
Photo.new(title: "Mountains", caption: "Mountains at sunset", url: "https://example.com/mountains.jpg")
]
render_inline(CollectionCounterComponent.with_collection(photos))

Expand All @@ -622,8 +622,8 @@ def test_render_collection_custom_collection_parameter_name_counter

def test_render_collection_custom_collection_parameter_name_iteration
photos = [
OpenStruct.new(title: "Flowers", caption: "Yellow flowers", url: "https://example.com/flowers.jpg"),
OpenStruct.new(title: "Mountains", caption: "Mountains at sunset", url: "https://example.com/mountains.jpg")
Photo.new(title: "Flowers", caption: "Yellow flowers", url: "https://example.com/flowers.jpg"),
Photo.new(title: "Mountains", caption: "Mountains at sunset", url: "https://example.com/mountains.jpg")
]
render_inline(CollectionIterationComponent.with_collection(photos))

Expand All @@ -636,8 +636,8 @@ def test_render_collection_custom_collection_parameter_name_iteration

def test_render_collection_custom_collection_parameter_name_iteration_extend_other_component
photos = [
OpenStruct.new(title: "Flowers", caption: "Yellow flowers", url: "https://example.com/flowers.jpg"),
OpenStruct.new(title: "Mountains", caption: "Mountains at sunset", url: "https://example.com/mountains.jpg")
Photo.new(title: "Flowers", caption: "Yellow flowers", url: "https://example.com/flowers.jpg"),
Photo.new(title: "Mountains", caption: "Mountains at sunset", url: "https://example.com/mountains.jpg")
]
render_inline(CollectionIterationExtendComponent.with_collection(photos))

Expand All @@ -650,8 +650,8 @@ def test_render_collection_custom_collection_parameter_name_iteration_extend_oth

def test_render_collection_custom_collection_parameter_name_iteration_extend_other_component_override
photos = [
OpenStruct.new(title: "Flowers", caption: "Yellow flowers", url: "https://example.com/flowers.jpg"),
OpenStruct.new(title: "Mountains", caption: "Mountains at sunset", url: "https://example.com/mountains.jpg")
Photo.new(title: "Flowers", caption: "Yellow flowers", url: "https://example.com/flowers.jpg"),
Photo.new(title: "Mountains", caption: "Mountains at sunset", url: "https://example.com/mountains.jpg")
]
render_inline(CollectionIterationExtendOverrideComponent.with_collection(photos))

Expand Down Expand Up @@ -680,7 +680,7 @@ def test_render_collection_missing_collection_object
end

def test_render_collection_missing_arg
products = [OpenStruct.new(name: "Radio clock"), OpenStruct.new(name: "Mints")]
products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")]
exception =
assert_raises ArgumentError do
render_inline(ProductComponent.with_collection(products))
Expand All @@ -691,7 +691,7 @@ def test_render_collection_missing_arg
end

def test_render_single_item_from_collection
product = OpenStruct.new(name: "Mints")
product = Product.new(name: "Mints")
render_inline(ProductComponent.new(product: product, notice: "On sale"))

assert_selector("h1", text: "Product", count: 1)
Expand All @@ -708,23 +708,23 @@ def test_collection_component_missing_parameter_name
def test_collection_component_missing_default_parameter_name
assert_raises ViewComponent::MissingCollectionArgumentError do
render_inline(
MissingDefaultCollectionParameterComponent.with_collection([OpenStruct.new(name: "Mints")])
MissingDefaultCollectionParameterComponent.with_collection([Product.new(name: "Mints")])
)
end
end

def test_collection_component_missing_custom_parameter_name_with_activemodel
assert_raises ViewComponent::MissingCollectionArgumentError do
render_inline(
MissingCollectionParameterWithActiveModelComponent.with_collection([OpenStruct.new(name: "Mints")])
MissingCollectionParameterWithActiveModelComponent.with_collection([Product.new(name: "Mints")])
)
end
end

def test_collection_component_present_custom_parameter_name_with_activemodel
assert_nothing_raised do
render_inline(
CollectionParameterWithActiveModelComponent.with_collection([OpenStruct.new(name: "Mints")])
CollectionParameterWithActiveModelComponent.with_collection([Product.new(name: "Mints")])
)
end
end
Expand Down
1 change: 0 additions & 1 deletion view_component.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bigdecimal"
spec.add_development_dependency "drb"
spec.add_development_dependency "mutex_m"
spec.add_development_dependency "ostruct"
end
end

0 comments on commit 812acf9

Please sign in to comment.