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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/view_model/active_record/controller_base.rb', line 146

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

  only_routes = []
  is_shallow = false
  resource resource_name, shallow: true, only: only_routes, **options do
    is_shallow = shallow_nesting_depth > 1
    instance_eval(&block) if block_given?

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

    if is_shallow
      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 is_shallow && 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



99
100
101
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
# File 'lib/view_model/active_record/controller_base.rb', line 99

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

  nested = shallow_nesting_depth > 0

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

  resources resource_name, shallow: true, only: only_routes, **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