-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert AttributeMethods into plugin
- Loading branch information
Showing
11 changed files
with
165 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Mobility | ||
module Plugins | ||
=begin | ||
Module builder adding translated attributes to #attributes hash on model | ||
instance. See {Mobility::Plugins::AttributeMethods} for further details. | ||
=end | ||
module ActiveRecord | ||
module TranslatedAttributes | ||
def translated_attributes | ||
{} | ||
end | ||
|
||
def attributes | ||
super.merge(translated_attributes) | ||
end | ||
end | ||
|
||
class AttributeMethods < Module | ||
def initialize(*attribute_names) | ||
include TranslatedAttributes | ||
define_method :translated_attributes do | ||
super().merge(attribute_names.inject({}) do |attributes, name| | ||
attributes.merge(name.to_s => send(name)) | ||
end) | ||
end | ||
delegate :translated_attribute_names, to: :class | ||
end | ||
|
||
def included(model_class) | ||
model_class.class_eval do | ||
define_method :untranslated_attributes, ::ActiveRecord::Base.instance_method(:attributes) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module Mobility | ||
module Plugins | ||
=begin | ||
Adds translated attribute names and values to the hash returned by #attributes. | ||
Also adds a method #translated_attributes with names and values of translated | ||
attributes only. | ||
@note Adding translated attributes to #attributes can have unexpected | ||
consequences, since these values are not true attribute values. | ||
=end | ||
module AttributeMethods | ||
class << self | ||
# Applies attribute_methods plugin for a given option value. | ||
# @param [Attributes] attributes | ||
# @param [Boolean] option Value of option | ||
# @raise [ArgumentError] if model class does not support dirty tracking | ||
def apply(attributes, option) | ||
if option | ||
include_attribute_methods_module(attributes.model_class, *attributes.names) | ||
end | ||
end | ||
|
||
private | ||
|
||
def include_attribute_methods_module(model_class, *attribute_names) | ||
module_builder = | ||
if Loaded::ActiveRecord && model_class.ancestors.include?(::ActiveRecord::AttributeMethods) | ||
require "mobility/plugins/active_record/attribute_methods" | ||
Plugins::ActiveRecord::AttributeMethods | ||
else | ||
raise ArgumentError, "#{model_class} does not support AttributeMethods plugin." | ||
end | ||
model_class.include module_builder.new(*attribute_names) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
spec/mobility/plugins/active_record/attribute_methods_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require "spec_helper" | ||
|
||
describe Mobility::Plugins::ActiveRecord::AttributeMethods, orm: :active_record do | ||
before do | ||
stub_const 'Article', Class.new(ActiveRecord::Base) | ||
Article.extend Mobility | ||
Article.class_eval do | ||
extend Mobility | ||
translates :title, backend: :null, attribute_methods: true | ||
|
||
def title | ||
"foo" | ||
end | ||
end | ||
end | ||
let(:untranslated_attributes) do | ||
{ | ||
"id" => nil, | ||
"slug" => nil, | ||
"published" => nil, | ||
"created_at" => nil, | ||
"updated_at" => nil | ||
} | ||
end | ||
|
||
subject { Article.new } | ||
|
||
describe "#translated_attribute_names" do | ||
it 'returns title' do | ||
expect(subject.translated_attribute_names).to include('title') | ||
end | ||
end | ||
|
||
describe "#translated_attributes" do | ||
it "returns hash of translated attribute names/values" do | ||
expect(subject.translated_attributes).to eq("title" => "foo") | ||
end | ||
end | ||
|
||
describe "#attributes" do | ||
it "adds translated attributes to normal attributes" do | ||
expect(subject.attributes).to eq( | ||
untranslated_attributes.merge("title" => "foo") | ||
) | ||
end | ||
end | ||
|
||
describe "#untranslated_attributes" do | ||
it "returns original value of attributes method" do | ||
expect(subject.untranslated_attributes).to eq(untranslated_attributes) | ||
end | ||
end | ||
end if Mobility::Loaded::ActiveRecord |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters