Module: Subroutine::Fields::ClassMethods

Defined in:
lib/subroutine/fields.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/subroutine/fields.rb', line 92

def method_missing(method_name, *args, &block)
  caster = ::Subroutine::TypeCaster.casters[method_name.to_sym]
  if caster
    field_name, options = args
    options ||= {}
    options[:type] = method_name.to_sym
    field(field_name, options)
  else
    super
  end
end

Instance Method Details

#field(field_name, options = {}) ⇒ Object Also known as: input



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/subroutine/fields.rb', line 32

def field(field_name, options = {})
  config = ::Subroutine::Fields::Configuration.from(field_name, options)
  config.validate!

  self.field_configurations = field_configurations.merge(field_name.to_sym => config)

  ensure_field_accessors(config)

  if config.groups.any?
    new_fields_by_group = self.fields_by_group.deep_dup

    config.groups.each do |group_name|
      new_fields_by_group[group_name] << config.field_name
      ensure_group_accessors(group_name)
    end

    self.fields_by_group = new_fields_by_group
  end

  config
end

#fields_from(*things) ⇒ Object Also known as: inputs_from



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/subroutine/fields.rb', line 55

def fields_from(*things)
  options = things.extract_options!
  excepts = options.key?(:except) ? Array(options.delete(:except)) : nil
  onlys = options.key?(:only) ? Array(options.delete(:only)) : nil

  things.each do |thing|
    local_excepts = excepts.map { |field| thing.get_field_config(field)&.related_field_names }.flatten.compact.uniq if excepts
    local_onlys = onlys.map { |field| thing.get_field_config(field)&.related_field_names }.flatten.compact.uniq if onlys

    thing.field_configurations.each_pair do |field_name, config|
      next if local_excepts&.include?(field_name)
      next if local_onlys && !local_onlys.include?(field_name)

      config.required_modules.each do |mod|
        include mod unless included_modules.include?(mod)
      end

      field(field_name, config.merge(options))
    end
  end
end

#get_field_config(field_name) ⇒ Object



78
79
80
# File 'lib/subroutine/fields.rb', line 78

def get_field_config(field_name)
  field_configurations[field_name.to_sym]
end

#include_defaults_in_params?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/subroutine/fields.rb', line 82

def include_defaults_in_params?
  return include_defaults_in_params unless include_defaults_in_params.nil?

  Subroutine.include_defaults_in_params?
end

#respond_to_missing?(method_name, *args, &block) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/subroutine/fields.rb', line 88

def respond_to_missing?(method_name, *args, &block)
  ::Subroutine::TypeCaster.casters.key?(method_name.to_sym) || super
end