forked from fixin.me/fixin.me
		
	Merging from main master to my repo master. #4
@ -1,16 +1,13 @@
 | 
				
			|||||||
class Default::UnitsController < ApplicationController
 | 
					class Default::UnitsController < ApplicationController
 | 
				
			||||||
  navigation_tab :units
 | 
					  navigation_tab :units
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  before_action -> { find_unit(current_user) }, only: :export
 | 
					  before_action :find_unit, only: [:import, :export, :destroy]
 | 
				
			||||||
  before_action -> { find_unit(nil) }, only: [:import, :destroy]
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  before_action except: :index do
 | 
					  before_action only: :import do
 | 
				
			||||||
    case action_name.to_sym
 | 
					    raise AccessForbidden unless current_user.at_least(:active)
 | 
				
			||||||
    when :import, :import_all
 | 
					  end
 | 
				
			||||||
      raise AccessForbidden unless current_user.at_least(:active)
 | 
					  before_action except: [:index, :import] do
 | 
				
			||||||
    else
 | 
					    raise AccessForbidden unless current_user.at_least(:admin)
 | 
				
			||||||
      raise AccessForbidden unless current_user.at_least(:admin)
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def index
 | 
					  def index
 | 
				
			||||||
@ -18,20 +15,19 @@ class Default::UnitsController < ApplicationController
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def import
 | 
					  def import
 | 
				
			||||||
    params = @unit.slice(Unit::ATTRIBUTES - [:symbol, :base_id])
 | 
					    raise ParameterInvalid unless @unit.default? && @unit.port(current_user)
 | 
				
			||||||
    current_user.units
 | 
					 | 
				
			||||||
      .find_or_initialize_by(symbol: @unit.symbol)
 | 
					 | 
				
			||||||
      .update!(base: @base, **params)
 | 
					 | 
				
			||||||
    run_and_render :index
 | 
					    run_and_render :index
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def import_all
 | 
					  #def import_all
 | 
				
			||||||
    # From defaults_diff return not only portability, but reason for not being
 | 
					    # From defaults_diff return not only portability, but reason for not being
 | 
				
			||||||
    # portable: missing_base and nesting_too_deep. Add portable and
 | 
					    # portable: missing_base and nesting_too_deep. Add portable and
 | 
				
			||||||
    # missing_base, if possible in one query
 | 
					    # missing_base, if possible in one query
 | 
				
			||||||
  end
 | 
					  #end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def export
 | 
					  def export
 | 
				
			||||||
 | 
					    raise ParameterInvalid unless !@unit.default? && @unit.port(nil)
 | 
				
			||||||
 | 
					    run_and_render :index
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def destroy
 | 
					  def destroy
 | 
				
			||||||
@ -39,8 +35,7 @@ class Default::UnitsController < ApplicationController
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  private
 | 
					  private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def find_unit(user)
 | 
					  def find_unit
 | 
				
			||||||
    @unit = Unit.find_by!(id: params[:id], user: user)
 | 
					    @unit = Unit.find_by!(id: params[:id], user: [current_user, nil])
 | 
				
			||||||
    @base = Unit.find_by!(symbol: @unit.base.symbol, user: user ? nil : current_user) if @unit.base
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
@ -24,6 +24,8 @@ class Unit < ApplicationRecord
 | 
				
			|||||||
    other_bases_units = arel_table.alias('other_bases_units')
 | 
					    other_bases_units = arel_table.alias('other_bases_units')
 | 
				
			||||||
    sub_units = arel_table.alias('sub_units')
 | 
					    sub_units = arel_table.alias('sub_units')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # TODO: move inner 'with' CTE to outer 'with recursive' - it can have multiple
 | 
				
			||||||
 | 
					    # CTEs, even non recursive ones.
 | 
				
			||||||
    Unit.with_recursive(actionable_units: [
 | 
					    Unit.with_recursive(actionable_units: [
 | 
				
			||||||
      Unit.with(units: self.or(Unit.defaults)).left_joins(:base)
 | 
					      Unit.with(units: self.or(Unit.defaults)).left_joins(:base)
 | 
				
			||||||
        .where.not(
 | 
					        .where.not(
 | 
				
			||||||
@ -90,7 +92,11 @@ class Unit < ApplicationRecord
 | 
				
			|||||||
    user_id.nil?
 | 
					    user_id.nil?
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def exportable?
 | 
					  def port(recipient)
 | 
				
			||||||
    !default? && (base.nil? || base.default?)
 | 
					    recipient_base = base && Unit.find_by(symbol: base.symbol, user: recipient)
 | 
				
			||||||
 | 
					    return nil if recipient_base.nil? != base.nil?
 | 
				
			||||||
 | 
					    params = slice(ATTRIBUTES - [:symbol, :base_id])
 | 
				
			||||||
 | 
					    Unit.find_or_initialize_by(user: recipient, symbol: symbol)
 | 
				
			||||||
 | 
					      .update(base: recipient_base, **params)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
@ -9,7 +9,7 @@ Rails.application.routes.draw do
 | 
				
			|||||||
  namespace :default do
 | 
					  namespace :default do
 | 
				
			||||||
    resources :units, only: [:index, :destroy] do
 | 
					    resources :units, only: [:index, :destroy] do
 | 
				
			||||||
      member { post :import, :export }
 | 
					      member { post :import, :export }
 | 
				
			||||||
      collection { post :import_all }
 | 
					      #collection { post :import_all }
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user