Module: ActionDispatch::Routing::Mapper::Resources

Defined in:
lib/view_model/active_record/controller_base.rb

Instance Method Summary collapse

Instance Method Details

#arvm_resource(resource_name, options = {}, &block) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/view_model/active_record/controller_base.rb', line 169

def arvm_resource(resource_name, options = {}, &block)
  except             = options.delete(:except) { [] }
  add_shallow_routes = options.delete(:add_shallow_routes) { true }
  defaults           = options.delete(:defaults) { {} }

  only_routes = []
  nested = shallow_nesting_depth > 0

  association_name = options.delete(:association_name) { resource_name.to_s }
  owner_viewmodel  = options.delete(:owner_viewmodel) do
    if nested
      parent_resource.name.to_s.singularize.camelize
    end
  end

  defaults = {
    association_name: association_name,
    owner_viewmodel: owner_viewmodel,
  }.merge(defaults)

  if nested && !except.include?(:create_associated_bulk)
    collection do
      post resource_name, controller: resource_name, action: :create_associated_bulk, as: nil, defaults: defaults
    end
  end

  resource resource_name, shallow: true, only: only_routes, defaults: defaults, **options do
    instance_eval(&block) if block_given?

    name_route = { as: '' } # Only one route may take the name

    if nested
      post('',   action: :create_associated,  **name_route.extract!(:as)) unless except.include?(:create)
      get('',    action: :show_associated,    **name_route.extract!(:as)) unless except.include?(:show)
      delete('', action: :destroy_associated, **name_route.extract!(:as)) unless except.include?(:destroy)
    else
      post('',   action: :create,  **name_route.extract!(:as)) unless except.include?(:create)
      get('',    action: :show,    **name_route.extract!(:as)) unless except.include?(:show)
      delete('', action: :destroy, **name_route.extract!(:as)) unless except.include?(:destroy)
    end
  end

  # singularly nested resources provide collection accessors at the top level
  if nested && add_shallow_routes
    resources resource_name.to_s.pluralize, shallow: true, only: [:show, :destroy] - except do
      shallow_scope do
        collection do
          name_route = { as: '' } # Only one route may take the name
          post('', action: :create, **name_route.extract!(:as)) unless except.include?(:create)
          get('',  action: :index,  **name_route.extract!(:as)) unless except.include?(:index)
        end
      end
    end
  end
end

#arvm_resources(resource_name, options = {}, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/view_model/active_record/controller_base.rb', line 102

def arvm_resources(resource_name, options = {}, &block)
  except             = options.delete(:except) { [] }
  add_shallow_routes = options.delete(:add_shallow_routes) { true }
  defaults           = options.delete(:defaults) { {} }

  nested = shallow_nesting_depth > 0

  association_name = options.delete(:association_name) { resource_name.to_s }
  owner_viewmodel  = options.delete(:owner_viewmodel) do
    if nested
      parent_resource.name.to_s.singularize.camelize
    end
  end

  defaults = {
    association_name: association_name,
    owner_viewmodel: owner_viewmodel,
  }.merge(defaults)

  only_routes = []
  only_routes += [:create] unless nested
  only_routes += [:show, :destroy] if add_shallow_routes
  only_routes -= except

  # Bulk replace
  if nested && !except.include?(:replace_bulk)
    collection do
      post resource_name, controller: resource_name, action: :replace_bulk, as: nil, defaults: defaults
    end
  end

  resources resource_name, shallow: true, only: only_routes, defaults: defaults, **options do
    instance_eval(&block) if block_given?

    if nested
      # Nested controllers also get :append and :disassociate, and alias a top level create.
      collection do
        name_route = { as: '' } # Only one route may take the name
        get('',    action: :index_associated, **name_route.extract!(:as)) unless except.include?(:index)
        put('',    action: :append,           **name_route.extract!(:as)) unless except.include?(:append)
        post('',   action: :replace,          **name_route.extract!(:as)) unless except.include?(:replace)
        delete('', action: :disassociate_all, **name_route.extract!(:as)) unless except.include?(:disassociate_all)
      end

      scope shallow: false do
        member do
          delete '', action: :disassociate, as: '' unless except.include?(:disassociate)
        end
      end

      # Add top level `create` route to manipulate existing viewmodels
      # without providing parent context
      shallow_scope do
        collection do
          name_route = { as: '' } # Only one route may take the name
          post('', action: :create, **name_route.extract!(:as)) unless except.include?(:create) || !add_shallow_routes
          get('',  action: :index,  **name_route.extract!(:as)) unless except.include?(:index)  || !add_shallow_routes
        end
      end
    else
      collection do
        get('', action: :index, as: '') unless except.include?(:index)
      end
    end
  end
end