Class: AspaceFormHelper::FormContext::JSONModelDefinition

Inherits:
BaseDefinition
  • Object
show all
Defined in:
frontend/app/helpers/aspace_form_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(jsonmodel, root) ⇒ JSONModelDefinition

Returns a new instance of JSONModelDefinition.



1031
1032
1033
1034
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1031

def initialize(jsonmodel, root)
  @jsonmodel = jsonmodel
  @root = root
end

Instance Method Details

#allowable_types_for(name) ⇒ Object



1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1059

def allowable_types_for(name)
  defn = jsonmodel_schema_definition(name)

  if defn
    ASUtils.extract_nested_strings(defn).map {|s|
      ref = JSONModel.parse_jsonmodel_ref(s)
      ref.first.to_s if ref
    }.compact
  else
    []
  end
end

#default_for(name) ⇒ Object



1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1043

def default_for(name)
  if jsonmodel_schema_definition(name)
    if jsonmodel_schema_definition(name).has_key?('dynamic_enum')
      if jsonmodel_schema_definition(name)['default']
        Rails.logger.warn("Superfluous default value at: #{@jsonmodel}.#{name} ")
      end
      JSONModel.enum_default_value(jsonmodel_schema_definition(name)['dynamic_enum'])
    else
      jsonmodel_schema_definition(name)['default']
    end
  else
    nil
  end
end

#options_for(context, property, add_empty_options = false, opts = {}) ⇒ Object



1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1073

def options_for(context, property, add_empty_options = false, opts = {})
  options = []
  options.push([(opts[:empty_label] || ""), ""]) if add_empty_options

  defn = jsonmodel_schema_definition(property)

  jsonmodel_enum_for(property).each do |v|
    if opts[:include] && !opts[:include].include?(v)
      next
    end
    if opts[:exclude] && opts[:exclude].include?(v)
      next
    end
    if opts.has_key?(:i18n_path_for) && opts[:i18n_path_for].has_key?(v)
      i18n_path = opts[:i18n_path_for][v]
    elsif opts.has_key?(:i18n_prefix)
      i18n_path = "#{opts[:i18n_prefix]}.#{v}"
    elsif defn.has_key?('dynamic_enum')
      i18n_path = {
        :enumeration =>  defn['dynamic_enum'],
        :value => v
      }
    else
      i18n_path = context.i18n_for("#{Array(property).last}_#{v}")
    end
    options.push([I18n.t(i18n_path, :default => v), v])
  end
  options
end

#required?(name) ⇒ Boolean

Returns:

  • (Boolean)


1037
1038
1039
1040
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1037

def required?(name)
  ((jsonmodel_schema_definition(name) &&
   jsonmodel_schema_definition(name)['ifmissing'] === 'error'))
end