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



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

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)
  @edit_fields.reject! { |f| f.name == fk_field.name }
  @index_fields.reject! { |f| f.name == fk_field.name }
  @edit_fields << fk_field
end

#exclude(*names) ⇒ Object



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

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



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/backstage/resource_config.rb', line 67

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
  else
    new_field = Field.new(sym, type || :string, opts)
    @edit_fields << new_field
    @index_fields << new_field
  end
end

#fields(*names) ⇒ Object



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

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

#has_many(name, **opts) ⇒ Object



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

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


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