Class: ClAdmin::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/cl_admin/resource.rb,
lib/cl_admin/resource/field.rb

Defined Under Namespace

Classes: Field

Constant Summary collapse

@@all_resources =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, name: nil, icon: nil, linked_on_dashboard: false, generate_routes: true, path: nil) ⇒ Resource

Returns a new instance of Resource.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cl_admin/resource.rb', line 9

def initialize(model:, name: nil, icon: nil, linked_on_dashboard: false, generate_routes: true, path: nil)
  @model = model
  @name = name || model.try(:model_name).try(:human)
  @linked_on_dashboard = linked_on_dashboard
  @icon = icon
  @link = '#'
  @subtitle = nil
  @generate_routes = generate_routes
  @path = path || plural_name.downcase

  @fields = []
  model.attribute_names.each do |attr|
    field = Field.new(
      attribute: attr,
      name: model.human_attribute_name(attr),
      create: default_columns.include?(attr),
      show: (model.attribute_names - ["id"]).include?(attr),
      index: (model.attribute_names - ["id"]).include?(attr),
      edit: default_columns.include?(attr)
    )
    @fields.push field
  end
  @index_fields ||= default_columns
  @form_fields ||= default_columns
  @show_fields ||= model.attribute_names - ["id"]
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def fields
  @fields
end

#form_fieldsObject (readonly)

Returns the value of attribute form_fields.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def form_fields
  @form_fields
end

#iconObject (readonly)

Returns the value of attribute icon.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def icon
  @icon
end

#index_fieldsObject (readonly)

Returns the value of attribute index_fields.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def index_fields
  @index_fields
end

Returns the value of attribute link.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def link
  @link
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def path
  @path
end

#show_fieldsObject (readonly)

Returns the value of attribute show_fields.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def show_fields
  @show_fields
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



7
8
9
# File 'lib/cl_admin/resource.rb', line 7

def subtitle
  @subtitle
end

Class Method Details

.allObject



65
66
67
# File 'lib/cl_admin/resource.rb', line 65

def self.all
  @@all_resources
end

.register(**args) ⇒ Object



60
61
62
63
# File 'lib/cl_admin/resource.rb', line 60

def self.register(**args)
  res = Resource.new(args)
  @@all_resources.push(res)
end

Instance Method Details

#default_columnsObject



56
57
58
# File 'lib/cl_admin/resource.rb', line 56

def default_columns
  model.attribute_names - ["id", "created_at", "updated_at"]
end

#generate_routes?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/cl_admin/resource.rb', line 73

def generate_routes?
  !!@generate_routes
end

#linked_on_dashboard?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/cl_admin/resource.rb', line 69

def linked_on_dashboard?
  !!@linked_on_dashboard
end

#pathsObject



44
45
46
47
48
49
50
# File 'lib/cl_admin/resource.rb', line 44

def paths
  @paths ||= {
    new: "new_#{model.name.underscore}_path",
    index: "#{plural_name.underscore}_path",
    edit: "edit_#{model.name.underscore}_path"
  }
end

#plural_nameObject



52
53
54
# File 'lib/cl_admin/resource.rb', line 52

def plural_name
  @plural_name ||= @name.pluralize
end