lib/avo/fields/belongs_to_field.rb in avo-0.5.0.beta9 vs lib/avo/fields/belongs_to_field.rb in avo-0.5.0.beta10
- old
+ new
@@ -1,17 +1,17 @@
module Avo
module Fields
class BelongsToField < BaseField
def initialize(name, **args, &block)
@defaults = {
- partial_name: 'belongs-to-field',
- placeholder: I18n.t('avo.choose_an_option')
+ partial_name: "belongs-to-field",
+ placeholder: I18n.t("avo.choose_an_option")
}
super(name, **args, &block)
- @meta[:searchable] = args[:searchable] == true ? true : false
+ @meta[:searchable] = args[:searchable] == true
@meta[:relation_method] = name.to_s.parameterize.underscore
end
def options
target_resource.model_class.all.map do |model|
@@ -26,11 +26,11 @@
target_resource.id
end
def foreign_key
if @model.present?
- if @model.class == Class
+ if @model.instance_of?(Class)
@model.reflections[@meta[:relation_method]].foreign_key
else
@model.class.reflections[@meta[:relation_method]].foreign_key
end
elsif @resource.present?
@@ -49,17 +49,18 @@
def to_permitted_param
foreign_key.to_sym
end
private
- def target_resource
- if @model._reflections[id.to_s].klass.present?
- App.get_resource_by_model_name @model._reflections[id.to_s].klass.to_s
- elsif @model._reflections[id.to_s].options[:class_name].present?
- App.get_resource_by_model_name @model._reflections[id.to_s].options[:class_name]
- else
- App.get_resource_by_name id.to_s
- end
+
+ def target_resource
+ if @model._reflections[id.to_s].klass.present?
+ App.get_resource_by_model_name @model._reflections[id.to_s].klass.to_s
+ elsif @model._reflections[id.to_s].options[:class_name].present?
+ App.get_resource_by_model_name @model._reflections[id.to_s].options[:class_name]
+ else
+ App.get_resource_by_name id.to_s
end
+ end
end
end
end