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.



7
8
9
10
11
12
13
14
15
# File 'lib/backstage/resource_config.rb', line 7

def initialize(model_class)
  @model_class = model_class
  @index_fields = []
  @edit_fields = []
  @associations = []
  @sidebar_links = []
  @custom_actions = []
  @excluded_columns = []
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

#custom_actionsObject

Returns the value of attribute custom_actions.



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

def custom_actions
  @custom_actions
end

#display_column(value = nil) ⇒ Object



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

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

#excluded_columnsObject

Returns the value of attribute excluded_columns.



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

def excluded_columns
  @excluded_columns
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.



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

def sidebar_config
  @sidebar_config
end

Returns the value of attribute sidebar_links.



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

def sidebar_links
  @sidebar_links
end

Instance Method Details

#belongs_to(name, **opts) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/backstage/resource_config.rb', line 59

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



38
39
40
41
42
# File 'lib/backstage/resource_config.rb', line 38

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/backstage/resource_config.rb', line 70

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



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

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

#has_many(name, **opts) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/backstage/resource_config.rb', line 44

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



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

def model_name_param
  model_class.model_name.plural
end

#row(*names) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/backstage/resource_config.rb', line 88

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



96
97
98
99
100
101
102
103
# File 'lib/backstage/resource_config.rb', line 96

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?
  @current_target = nil
  @edit_fields << section_field
end


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

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