Skip to content

Commit

Permalink
Convert OpenStruct to Data
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Oct 1, 2024
1 parent f97353a commit 328566f
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 13 deletions.
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PATH
specs:
madmin (1.2.11)
importmap-rails
ostruct
pagy (>= 3.5)
propshaft
rails (>= 7.0.0)
Expand Down Expand Up @@ -150,7 +149,6 @@ GEM
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
racc (~> 1.4)
ostruct (0.6.0)
pagy (9.0.6)
paper_trail (15.1.0)
activerecord (>= 6.1)
Expand Down
9 changes: 4 additions & 5 deletions app/views/madmin/fields/nested_has_many/_fields.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<%= content_tag :div, class: "nested-fields border border-gray-200 rounded-lg p-5", data: { new_record: f.object.new_record? } do %>
<% field.nested_attributes.each do |name, nested_attribute| %>
<% next if nested_attribute[:field].nil? %>
<% next unless nested_attribute[:field].visible?(action_name) %>
<% next unless nested_attribute[:field].visible?(:form) %>
<% nested_field = nested_attribute[:field] %>
<% nested_field = nested_attribute.field %>
<% next if nested_field.nil? %>
<% next unless nested_field.visible?(action_name) %>
<% next unless nested_field.visible?(:form) %>

<div class="mb-4 flex">
<%= render partial: nested_field.to_partial_path("form"), locals: { field: nested_field, record: f.object, form: f, resource: field.resource } %>
Expand Down
6 changes: 3 additions & 3 deletions lib/madmin/resource.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "ostruct"

module Madmin
class Resource
Attribute = Data.define(:name, :type, :field)

class_attribute :attributes, default: ActiveSupport::OrderedHash.new
class_attribute :member_actions, default: []
class_attribute :scopes, default: []
Expand Down Expand Up @@ -55,7 +55,7 @@ def attribute(name, type = nil, **options)
config.update = value
end

attributes[name] = OpenStruct.new(
attributes[name] = Attribute.new(
name: name,
type: type,
field: field.new(attribute_name: name, model: model, resource: self, options: config)
Expand Down
2 changes: 1 addition & 1 deletion lib/madmin/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def query_template
end

def searchable_fields(attr)
[attr[:name]]
[attr.name]
end

def query_values
Expand Down
1 change: 0 additions & 1 deletion madmin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ Gem::Specification.new do |spec|
spec.add_dependency "pagy", ">= 3.5"
spec.add_dependency "importmap-rails"
spec.add_dependency "propshaft"
spec.add_dependency "ostruct"
end
2 changes: 1 addition & 1 deletion test/madmin/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class FooBarBazResource < Madmin::Resource; end

class ResourceTest < ActiveSupport::TestCase
test "searchable_attributes" do
searchable_attribute_names = UserResource.searchable_attributes.map { |a| a[:name] }
searchable_attribute_names = UserResource.searchable_attributes.map(&:name)
assert_includes searchable_attribute_names, :first_name
end

Expand Down

0 comments on commit 328566f

Please sign in to comment.