Class: Backstage::ResourceConfig
- Inherits:
-
Object
- Object
- Backstage::ResourceConfig
- Defined in:
- lib/backstage/resource_config.rb
Instance Attribute Summary collapse
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#custom_actions ⇒ Object
Returns the value of attribute custom_actions.
- #display_column(value = nil) ⇒ Object
-
#edit_fields ⇒ Object
Returns the value of attribute edit_fields.
-
#excluded_columns ⇒ Object
Returns the value of attribute excluded_columns.
-
#index_fields ⇒ Object
Returns the value of attribute index_fields.
-
#model_class ⇒ Object
Returns the value of attribute model_class.
-
#sidebar_config ⇒ Object
readonly
Returns the value of attribute sidebar_config.
-
#sidebar_links ⇒ Object
Returns the value of attribute sidebar_links.
Instance Method Summary collapse
- #belongs_to(name, **opts) ⇒ Object
- #exclude(*names) ⇒ Object
- #field(name, **opts) ⇒ Object
- #fields(*names) ⇒ Object
- #has_many(name, **opts) ⇒ Object
-
#initialize(model_class) ⇒ ResourceConfig
constructor
A new instance of ResourceConfig.
- #model_name_param ⇒ Object
- #sidebar(&block) ⇒ Object
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
#associations ⇒ Object
Returns the value of attribute associations.
3 4 5 |
# File 'lib/backstage/resource_config.rb', line 3 def associations @associations end |
#custom_actions ⇒ Object
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_fields ⇒ Object
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_columns ⇒ Object
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_fields ⇒ Object
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_class ⇒ Object
Returns the value of attribute model_class.
3 4 5 |
# File 'lib/backstage/resource_config.rb', line 3 def model_class @model_class end |
#sidebar_config ⇒ Object (readonly)
Returns the value of attribute sidebar_config.
31 32 33 |
# File 'lib/backstage/resource_config.rb', line 31 def @sidebar_config end |
#sidebar_links ⇒ Object
Returns the value of attribute sidebar_links.
3 4 5 |
# File 'lib/backstage/resource_config.rb', line 3 def @sidebar_links end |
Instance Method Details
#belongs_to(name, **opts) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# 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) 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 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
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/backstage/resource_config.rb', line 69 def field(name, **opts) type = opts.delete(:as) || opts.delete(:type) sym = name.to_sym existing = find_field(sym) if existing existing..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_param ⇒ Object
21 22 23 |
# File 'lib/backstage/resource_config.rb', line 21 def model_name_param model_class.model_name.plural end |
#sidebar(&block) ⇒ Object
25 26 27 28 29 |
# File 'lib/backstage/resource_config.rb', line 25 def (&block) @sidebar_config ||= SidebarConfig.new block&.call(@sidebar_config) @sidebar_config end |