-
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
9 changed files
with
93 additions
and
97 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,31 @@ | ||
module Mobility | ||
module Plugins | ||
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({}) { |attributes, name| attributes.merge(name.to_s => send(name)) }) | ||
end | ||
end | ||
|
||
def included(model_class) | ||
untranslated_attributes = model_class.superclass.instance_method(:attributes) | ||
model_class.class_eval do | ||
define_method :untranslated_attributes, untranslated_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,30 @@ | ||
module Mobility | ||
module Plugins | ||
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.