Skip to content

Commit e5ca5ad

Browse files
committed
fix: ensure the stored field names are always symbols
There is code that assumes that `@field_names` will contain symbols, like this method in ActiveHash::Base: def respond_to?(method_name, include_private=false) super || begin config = configuration_for_custom_finder(method_name) config && config[:fields].all? do |field| field_names.include?(field.to_sym) || field.to_sym == :id end end end
1 parent 24579e9 commit e5ca5ad

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/active_hash/base.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ def fields(*args)
198198
end
199199

200200
def field(field_name, options = {})
201+
field_name = field_name.to_sym
201202
validate_field(field_name)
203+
202204
field_names << field_name
203205

204206
add_default_value(field_name, options[:default]) if options.key?(:default)
@@ -210,7 +212,8 @@ def field(field_name, options = {})
210212
end
211213

212214
def validate_field(field_name)
213-
if [:attributes].include?(field_name.to_sym)
215+
field_name = field_name.to_sym
216+
if [:attributes].include?(field_name)
214217
raise ReservedFieldError.new("#{field_name} is a reserved field in ActiveHash. Please use another name.")
215218
end
216219
end
@@ -264,7 +267,7 @@ def add_default_value field_name, default_value
264267
end
265268

266269
def define_getter_method(field, default_value)
267-
unless instance_methods.include?(field.to_sym)
270+
unless instance_methods.include?(field)
268271
define_method(field) do
269272
attributes[field].nil? ? default_value : attributes[field]
270273
end

spec/active_hash/base_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ class Country < ActiveHash::Base
106106
end
107107
end
108108

109+
describe ".field_names" do
110+
before do
111+
Country.fields :name, :iso_name, "size"
112+
end
113+
114+
it "returns an array of field names" do
115+
expect(Country.field_names).to eq([:name, :iso_name, :size])
116+
end
117+
end
118+
109119
describe ".data=" do
110120
before do
111121
class Region < ActiveHash::Base

0 commit comments

Comments
 (0)