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

Add column_affix when configuring options #217

Merged
merged 1 commit into from
Apr 29, 2018
Merged
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
3 changes: 1 addition & 2 deletions lib/mobility/backends/active_record/pg_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def translations
end

setup do |attributes, options = {}|
affix = "#{options[:column_prefix]}%s#{options[:column_suffix]}"
attributes.each { |attribute| store (affix % attribute), coder: Coder }
attributes.each { |attribute| store (options[:column_affix] % attribute), coder: Coder }
end

class Coder
Expand Down
2 changes: 1 addition & 1 deletion lib/mobility/backends/active_record/pg_query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module PgQueryMethods
def initialize(attributes, options)
super
@arel_table = options[:model_class].arel_table
@column_affix = "#{options[:column_prefix]}%s#{options[:column_suffix]}"
@column_affix = options[:column_affix]

q = self

Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/active_record/serialized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ class ActiveRecord::Serialized
# @option (see Backends::Serialized.configure)
# @raise (see Backends::Serialized.configure)
def self.configure(options)
super
Serialized.configure(options)
end
# @!endgroup

setup do |attributes, options|
coder = { yaml: YAMLCoder, json: JSONCoder }[options[:format]]
column_affix = "#{options[:column_prefix]}%s#{options[:column_suffix]}"
attributes.each { |attribute| serialize (column_affix % attribute), coder }
attributes.each { |attribute| serialize (options[:column_affix] % attribute), coder }
end

setup_query_methods(QueryMethods)
Expand Down
12 changes: 11 additions & 1 deletion lib/mobility/backends/hash_valued.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module HashValued
# name from attribute name
def initialize(_model, _attribute, options = {})
super
@column_affix = "#{options[:column_prefix]}%s#{options[:column_suffix]}"
@column_affix = options[:column_affix]
end

# @!group Backend Accessors
Expand All @@ -36,6 +36,16 @@ def each_locale
translations.each { |l, _| yield l }
end

def self.included(backend_class)
backend_class.extend ClassMethods
end

module ClassMethods
def configure(options)
options[:column_affix] = "#{options[:column_prefix]}%s#{options[:column_suffix]}"
end
end

private

def column_name
Expand Down
5 changes: 2 additions & 3 deletions lib/mobility/backends/sequel/pg_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def translations
end

setup do |attributes, options|
column_affix = "#{options[:column_prefix]}%s#{options[:column_suffix]}"
columns = attributes.map { |attribute| (column_affix % attribute).to_sym }
columns = attributes.map { |attribute| (options[:column_affix] % attribute).to_sym }

before_validation = Module.new do
define_method :before_validation do
Expand All @@ -48,7 +47,7 @@ def translations
end
include before_validation
include Mobility::Sequel::HashInitializer.new(*columns)
include Mobility::Sequel::ColumnChanges.new(attributes, column_affix: column_affix)
include Mobility::Sequel::ColumnChanges.new(attributes, column_affix: options[:column_affix])

plugin :defaults_setter
columns.each { |column| default_values[column] = {} }
Expand Down
2 changes: 1 addition & 1 deletion lib/mobility/backends/sequel/pg_query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module PgQueryMethods

def initialize(attributes, options)
super
@column_affix = "#{options[:column_prefix]}%s#{options[:column_suffix]}"
@column_affix = options[:column_affix]
define_query_methods
end

Expand Down
4 changes: 2 additions & 2 deletions lib/mobility/backends/sequel/serialized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ class Sequel::Serialized
# @option (see Backends::Serialized.configure)
# @raise (see Backends::Serialized.configure)
def self.configure(options)
super
Serialized.configure(options)
end
# @!endgroup

setup do |attributes, options|
format = options[:format]
column_affix = "#{options[:column_prefix]}%s#{options[:column_suffix]}"
columns = attributes.map { |attribute| (column_affix % attribute).to_sym }
columns = attributes.map { |attribute| (options[:column_affix] % attribute).to_sym }

plugin :serialization
plugin :serialization_modification_detection
Expand Down