Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing translated attributes in as_json #630

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix missing translated attributes in as_json with attribute_methods p…
…lugin enabled
  • Loading branch information
scarfacedeb committed Nov 7, 2023
commit f8b107caa59883a6e254652e2254fb18348d5115
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
Loading