Class: ActiveAdmin::Localize::Field
- Inherits:
-
Object
- Object
- ActiveAdmin::Localize::Field
- Extended by:
- ActiveModel::Naming
- Defined in:
- lib/activeadmin-localize/field.rb
Constant Summary collapse
- @@validators =
{}
- @@model =
nil
Class Method Summary collapse
- .before_validation ⇒ Object
- .human_attribute_name(*args) ⇒ Object
- .validators_on(attributized_method_name) ⇒ Object
Instance Method Summary collapse
- #clean_locale(locale) ⇒ Object
- #errors ⇒ Object
-
#initialize(obj, name) ⇒ Field
constructor
A new instance of Field.
- #method_missing(*args) ⇒ Object
- #required? ⇒ Boolean
- #respond_to?(method_name, include_private = false) ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize(obj, name) ⇒ Field
Returns a new instance of Field.
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 |
# File 'lib/activeadmin-localize/field.rb', line 14 def initialize(obj, name) @obj = obj @@model = obj.class @field = name @@validators[@field] = @obj.class.validators_on(name.to_sym) validators = @@validators[@field].map(&:class).map(&:name) @required = validators.include?('ActiveRecord::Validations::PresenceValidator') || validators.include?('Mongoid::Validations::PresenceValidator') @errors = ActiveModel::Errors.new(self) hash = @obj.send("#{name}_translations") hash = {} if hash.nil? ::ActiveAdmin::Localize.locales.each do |k| inst_var_name = clean_locale(k) ## create the getter that returns the instance variable self.class.send(:define_method, k, proc{self.instance_variable_get("@#{inst_var_name}")}) ## create the setter that sets the instance variable self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{inst_var_name}", v)}) ## create and initialize an instance variable for this key/value pair self.instance_variable_set("@#{inst_var_name}", '') end hash.each do |k,v| ## create and initialize an instance variable for this key/value pair inst_var_name = k.to_s.gsub('-','_') self.instance_variable_set("@#{inst_var_name}", v) end def @obj.before_validation validate! end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
66 67 68 |
# File 'lib/activeadmin-localize/field.rb', line 66 def method_missing(*args) @obj.send(*args) end |
Class Method Details
.before_validation ⇒ Object
44 45 46 |
# File 'lib/activeadmin-localize/field.rb', line 44 def @obj.before_validation validate! end |
.human_attribute_name(*args) ⇒ Object
78 79 80 |
# File 'lib/activeadmin-localize/field.rb', line 78 def self.human_attribute_name(*args) @@model.send(:human_attribute_name, *args) end |
.validators_on(attributized_method_name) ⇒ Object
74 75 76 |
# File 'lib/activeadmin-localize/field.rb', line 74 def self.validators_on(attributized_method_name) @@validators[attributized_method_name].nil? ? [] : @@validators[attributized_method_name] end |
Instance Method Details
#clean_locale(locale) ⇒ Object
10 11 12 |
# File 'lib/activeadmin-localize/field.rb', line 10 def clean_locale(locale) locale.to_s.gsub('-','_') end |
#errors ⇒ Object
53 54 55 56 |
# File 'lib/activeadmin-localize/field.rb', line 53 def errors validate! if @obj.errors.any? @errors end |
#required? ⇒ Boolean
49 50 51 |
# File 'lib/activeadmin-localize/field.rb', line 49 def required? @required end |
#respond_to?(method_name, include_private = false) ⇒ Boolean
70 71 72 |
# File 'lib/activeadmin-localize/field.rb', line 70 def respond_to?(method_name, include_private = false) @obj.respond_to?(method_name, include_private) end |
#validate! ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/activeadmin-localize/field.rb', line 58 def validate! ::ActiveAdmin::Localize.locales.each do |k| if @required && send(k).blank? @errors.add(k, I18n.t('errors.messages.blank')) end end end |