Class: Backstage::ResourceConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/backstage/resource_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ ResourceConfig

Returns a new instance of ResourceConfig.



6
7
8
9
10
11
# File 'lib/backstage/resource_config.rb', line 6

def initialize(model_class)
  @model_class = model_class
  @index_fields = []
  @edit_fields = []
  @associations = []
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



3
4
5
# File 'lib/backstage/resource_config.rb', line 3

def associations
  @associations
end

#display_column(value = nil) ⇒ Object



13
14
15
# File 'lib/backstage/resource_config.rb', line 13

def display_column(value = nil)
  value ? @display_column = value.to_sym : @display_column
end

#edit_fieldsObject

Returns the value of attribute edit_fields.



3
4
5
# File 'lib/backstage/resource_config.rb', line 3

def edit_fields
  @edit_fields
end

#index_fieldsObject

Returns the value of attribute index_fields.



3
4
5
# File 'lib/backstage/resource_config.rb', line 3

def index_fields
  @index_fields
end

#model_classObject

Returns the value of attribute model_class.



3
4
5
# File 'lib/backstage/resource_config.rb', line 3

def model_class
  @model_class
end

Returns the value of attribute sidebar_config.



27
28
29
# File 'lib/backstage/resource_config.rb', line 27

def sidebar_config
  @sidebar_config
end

Instance Method Details

#belongs_to(name, **opts) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/backstage/resource_config.rb', line 63

def belongs_to(name, **opts)
  assoc = AssociationConfig.new(name, :belongs_to, opts)
  @associations << assoc
  fk_field = Field.new(assoc.foreign_key, :belongs_to, association: assoc)
  index_field = Field.new(name.to_sym, :belongs_to, association: assoc)
  @edit_fields.reject! { |f| f.name == fk_field.name }
  @index_fields.reject! { |f| f.name == fk_field.name || f.name == index_field.name }
  @edit_fields << fk_field
  @index_fields << index_field unless @index_fields_explicit
end

#exclude(*names) ⇒ Object



34
35
36
37
38
# File 'lib/backstage/resource_config.rb', line 34

def exclude(*names)
  names.map!(&:to_sym)
  @index_fields = @index_fields.reject { |f| names.include?(f.name) }
  @edit_fields = @edit_fields.reject { |f| names.include?(f.name) }
end

#field(name, **opts) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/backstage/resource_config.rb', line 74

def field(name, **opts)
  type = opts.delete(:as) || opts.delete(:type)
  sym = name.to_sym
  existing = find_field(sym)
  if existing
    existing.options.merge!(opts)
    existing.instance_variable_set(:@type, type.to_sym) if type
    if @current_target
      @edit_fields.reject! { |f| f.name == sym }
      @current_target << existing
    end
  else
    new_field = Field.new(sym, type || :string, opts)
    (@current_target || @edit_fields) << new_field
    @index_fields << new_field unless @index_fields_explicit || @current_target
  end
end

#fields(*names) ⇒ Object



29
30
31
32
# File 'lib/backstage/resource_config.rb', line 29

def fields(*names)
  @index_fields_explicit = true
  @index_fields = names.map { |n| find_or_build_field(n) }
end

#has_many(name, **opts) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/backstage/resource_config.rb', line 40

def has_many(name, **opts)
  display_as = opts.delete(:as) || :has_many
  assoc = AssociationConfig.new(name, :has_many, opts)
  @associations << assoc
  if display_as == :thumbnails
    field_obj = Field.new(name.to_sym, :thumbnails, association: assoc, readonly: true)
    @edit_fields.reject! { |f| f.name == field_obj.name }
    @edit_fields << field_obj
  else
    ids_field = Field.new(:"#{name.to_s.singularize}_ids", :has_many, association: assoc)
    @edit_fields.reject! { |f| f.name == ids_field.name }
    @edit_fields << ids_field
  end
end

#model_name_paramObject



17
18
19
# File 'lib/backstage/resource_config.rb', line 17

def model_name_param
  model_class.model_name.plural
end

#nested(name, fields:, readonly_fields: []) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/backstage/resource_config.rb', line 55

def nested(name, fields:, readonly_fields: [])
  field_obj = Field.new(name.to_sym, :nested,
    nested_fields: Array(fields).map(&:to_sym),
    nested_readonly_fields: Array(readonly_fields).map(&:to_sym))
  @edit_fields.reject! { |f| f.name == field_obj.name }
  (@current_target || @edit_fields) << field_obj
end

#row(*names) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/backstage/resource_config.rb', line 92

def row(*names)
  syms = names.map(&:to_sym)
  sub = syms.map { |n| find_field(n) || Field.new(n, :string) }
  @edit_fields.reject! { |f| syms.include?(f.name) }
  row_field = Field.new(:"row_#{names.first}", :row, sub_fields: sub)
  (@current_target || @edit_fields) << row_field
end

#section(heading, **opts) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/backstage/resource_config.rb', line 100

def section(heading, **opts)
  section_field = Field.new(:"section_#{heading.parameterize}", :section,
    heading: heading, sub_fields: [], **opts)
  @current_target = section_field.sub_fields
  yield if block_given?
  @edit_fields << section_field
ensure
  @current_target = nil
end


21
22
23
24
25
# File 'lib/backstage/resource_config.rb', line 21

def sidebar(&block)
  @sidebar_config ||= SidebarConfig.new
  block&.call(@sidebar_config)
  @sidebar_config
end