Module: Armot::ActiveRecordExtensions::ClassMethods

Defined in:
lib/armot/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#armotize(*attributes) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/armot/active_record_extensions.rb', line 4

def armotize(*attributes)
  if included_modules.include?(InstanceMethods)
    raise DoubleDeclarationError, "armotize can only be called once in #{self}"
  end

  make_it_armot!

  instance_mixin = Module.new
  class_mixin = Module.new

  class_mixin.module_eval do
    define_method :armotized_attributes do
      attributes.map(&:to_sym)
    end

    define_method :define_localized_accessors_for do |*localizable_attributes|
      reload_localized_accessors_for *localizable_attributes
    end

    define_method :reload_localized_accessors_for do |*localizable_attributes|
      localizable_attributes = armotized_attributes if localizable_attributes.first == :all

      locales_to_define = if localizable_attributes.last.is_a?(Hash) && localizable_attributes.last[:locales]
        localizable_attributes.last[:locales]
      else
        I18n.available_locales
      end

      localizable_attributes.each do |attr|
        locales_to_define.each do |locale|
          next if respond_to?(:"#{attr}_#{locale}")
          define_method "#{attr}_#{locale}" do
            armot_wrap_in_locale(locale) do
              send attr
            end
          end

          next if respond_to?(:"#{attr}_#{locale}=")
          define_method "#{attr}_#{locale}=" do |value|
            armot_wrap_in_locale(locale) do
              send "#{attr}=", value
            end
          end
        end
      end
    end
  end

  attributes.each do |attribute|
    class_mixin.module_eval do
      define_method :"find_by_#{attribute}" do |value|
        t = I18n::Backend::ActiveRecord::Translation.arel_table
        trans = I18n::Backend::ActiveRecord::Translation.where(
          :locale => I18n.locale,
          :value => value.to_yaml
        ).where(
          t[:key].matches("armot.#{self.to_s.underscore.pluralize}%")
        ).all

        return send("where", {:"#{attribute}" => value}).first if trans.empty?

        res = nil
        trans.each do |x|
          res = find_by_id x.key.split("_").last
          break if res
        end

        res
      end

      define_method :"find_by_#{attribute}!" do |value|
        t = I18n::Backend::ActiveRecord::Translation.arel_table
        trans = I18n::Backend::ActiveRecord::Translation.where(
          :locale => I18n.locale,
          :value => value.to_yaml
        ).where(
          t[:key].matches("armot.#{self.to_s.underscore.pluralize}%")
        ).all

        if trans.empty?
          original = send("where", {:"#{attribute}" => value}).first
          raise ActiveRecord::RecordNotFound if original.nil?
          original
        else
          res = nil
          trans.each do |x|
            res = find_by_id x.key.split("_").last
            break if res
          end

          res ? res : raise(ActiveRecord::RecordNotFound)
        end
      end
    end

    instance_mixin.module_eval do
      define_method :"#{attribute}=" do |value|
        armot_attributes[I18n.locale]["#{attribute}"] = value
      end

      define_method :"#{attribute}_changed?" do
        armot_attributes[I18n.locale]["#{attribute}"].present?
      end

      define_method :"#{attribute}_raw" do
        return armot_attributes[I18n.locale]["#{attribute}"]
      end

      define_method :"#{attribute}" do
        return send("#{attribute}_raw") if armot_attributes[I18n.locale]["#{attribute}"]

        if armot_attributes.any? && I18n.respond_to?(:fallbacks)
          I18n.fallbacks[I18n.locale].each do |fallback|
            return armot_attributes[fallback]["#{attribute}"] if armot_attributes[fallback]["#{attribute}"]
          end
        end

        if persisted? && I18n.respond_to?(:fallbacks)
          trans = I18n.t "#{attribute}_#{id}", :scope => "armot.#{self.class.to_s.underscore.pluralize}.#{attribute}", :default => Armot.token
          return trans if trans != Armot.token
        end

        ( new_record? || trans == Armot.token) ? self[:"#{attribute}"] : trans
      end
    end
  end

  self.const_set("ArmotInstanceMethods", instance_mixin)
  self.const_set("ArmotClassMethods", class_mixin)
  include self.const_get("ArmotInstanceMethods") unless self.included_modules.map(&:to_s).include?("#{self}::ArmotInstanceMethods")
  extend self.const_get("ArmotClassMethods") unless self.singleton_class.included_modules.map(&:to_s).include?("#{self}::ArmotClassMethods")
end

#validates_armotized_presence_of(attr_name, locales) ⇒ Object



137
138
139
# File 'lib/armot/active_record_extensions.rb', line 137

def validates_armotized_presence_of(attr_name, locales)
  validates_with Armot::ActiveRecordExtensions::Validations::PresenceValidator, :attr => attr_name, :locales => locales
end