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.



1050
1051
1052
1053
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1050

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

Instance Method Details

#allowable_types_for(name) ⇒ Object



1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1078

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



1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1062

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



1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1092

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

#record_typeObject



1122
1123
1124
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1122

def record_type
  @jsonmodel.record_type
end

#required?(name) ⇒ Boolean

Returns:

  • (Boolean)


1056
1057
1058
1059
# File 'frontend/app/helpers/aspace_form_helper.rb', line 1056

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