Class: Ez::Resources::Manager::Config

Inherits:
Object
  • Object
show all
Includes:
Pagy::Backend
Defined in:
lib/ez/resources/manager/config.rb

Constant Summary collapse

DEFAULT_ACTIONS =
%i[index show new create edit update destroy].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller:, dsl_config:, data: nil) ⇒ Config

Returns a new instance of Config.



13
14
15
16
17
# File 'lib/ez/resources/manager/config.rb', line 13

def initialize(controller:, dsl_config:, data: nil)
  @controller = controller
  @dsl_config = dsl_config
  @data       = data
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



11
12
13
# File 'lib/ez/resources/manager/config.rb', line 11

def controller
  @controller
end

#paginatorObject (readonly)

Returns the value of attribute paginator.



11
12
13
# File 'lib/ez/resources/manager/config.rb', line 11

def paginator
  @paginator
end

#searchObject (readonly)

Returns the value of attribute search.



11
12
13
# File 'lib/ez/resources/manager/config.rb', line 11

def search
  @search
end

Instance Method Details

#actionsObject



43
44
45
# File 'lib/ez/resources/manager/config.rb', line 43

def actions
  @actions ||= dsl_config.actions || DEFAULT_ACTIONS
end

#collection_actionsObject



89
90
91
# File 'lib/ez/resources/manager/config.rb', line 89

def collection_actions
  @colleciton_actions ||= dsl_config.collection_actions || []
end

#collection_columnsObject



79
80
81
82
83
84
85
86
87
# File 'lib/ez/resources/manager/config.rb', line 79

def collection_columns
  @collection_columns ||= dsl_config.collection_columns || model.columns.map do |column|
    Ez::Resources::Manager::Field.new(
      name:  column.name,
      title: column.name.to_s.humanize,
      type:  column..type
    )
  end.reject { |col| Ez::Resources.config.ignore_fields.include?(col.name) }
end

#collection_search?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ez/resources/manager/config.rb', line 71

def collection_search?
  @collection_search ||= dsl_config.collection_search != false
end

#collection_viewsObject



75
76
77
# File 'lib/ez/resources/manager/config.rb', line 75

def collection_views
  @collection_views ||= dsl_config.collection_views || []
end

#dataObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ez/resources/manager/config.rb', line 19

def data
  @data ||= case controller.action_name
            when 'index'   then collection
            when 'new'     then new_resource
            when 'show'    then resource
            when 'edit'    then resource
            when 'update'  then resource
            when 'destroy' then resource
            else
              raise ConfigurationError, "Invalid action #{controller.action_name}"
            end
end

#form_fieldsObject



93
94
95
# File 'lib/ez/resources/manager/config.rb', line 93

def form_fields
  @form_fields ||= dsl_config.form_fields || collection_columns || []
end

#hooksObject



51
52
53
# File 'lib/ez/resources/manager/config.rb', line 51

def hooks
  @hooks ||= dsl_config.hooks || []
end

#modelObject



36
37
38
39
40
41
# File 'lib/ez/resources/manager/config.rb', line 36

def model
  @model ||= dsl_config.model || controller_name.classify.constantize
rescue NameError
  raise GuessingError,
        "Ez::Resources tried to guess model name as #{controller_name.classify} but constant is missing. You can define model class explicitly with :model options"
end

#paginate_collection?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/ez/resources/manager/config.rb', line 67

def paginate_collection?
  @paginate_collection ||= dsl_config.paginate_collection != false
end

#paramsObject



107
108
109
# File 'lib/ez/resources/manager/config.rb', line 107

def params
  @params ||= controller.params
end

#path_for(action:, id: nil, params: nil) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/ez/resources/manager/config.rb', line 97

def path_for(action:, id: nil, params: nil)
  if id
    controller.url_for(action: action, id: id, only_path: true)
  elsif params
    controller.url_for(action: action, **params, only_path: true)
  else
    controller.url_for(action: action, only_path: true)
  end
end

#resource_labelObject



55
56
57
# File 'lib/ez/resources/manager/config.rb', line 55

def resource_label
  @resource_label ||= dsl_config.resource_label || :id
end

#resource_nameObject



59
60
61
# File 'lib/ez/resources/manager/config.rb', line 59

def resource_name
  @resource_name ||= controller_name.classify
end

#resources_nameObject



63
64
65
# File 'lib/ez/resources/manager/config.rb', line 63

def resources_name
  @resources_name ||= dsl_config.resources_name || resource_name.pluralize
end

#show_action_renders_form?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/ez/resources/manager/config.rb', line 47

def show_action_renders_form?
  @show_action_renders_form ||= dsl_config.show_action_renders_form
end

#total_countObject



32
33
34
# File 'lib/ez/resources/manager/config.rb', line 32

def total_count
  @total_count ||= model.count
end