Skip to content

Commit

Permalink
add to_json support
Browse files Browse the repository at this point in the history
  • Loading branch information
bonekost committed Jul 23, 2024
1 parent 31b8a9b commit c7e7404
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/mobility/plugins/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ module AttributeMethods
attributes.merge(name.to_s => send(name))
end)
end

private

define_method :attribute_names_for_serialization do
return unless defined?(super)

super() + names.map(&:to_s)
end
end

# Applies attribute_methods plugin for a given option value.
Expand Down
7 changes: 7 additions & 0 deletions spec/mobility/plugins/attribute_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
end
end

describe "#as_json" do
it "adds translated attributes to normal attributes" do
expect(backend).to receive(:read).with(Mobility.locale, any_args).and_return('foo').at_least(1)
expect(instance.as_json).to eq(untranslated_attributes.merge('title' => 'foo'))
end
end

describe "#untranslated_attributes" do
it "returns original value of attributes method" do
expect(instance.untranslated_attributes).to eq(untranslated_attributes)
Expand Down

0 comments on commit c7e7404

Please sign in to comment.