diff --git a/.rubocop.yml b/.rubocop.yml index 9a9c0c4ea..48a00e17b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -188,7 +188,7 @@ SpecialGlobalVars: Enabled: false StringLiterals: - EnforcedStyle: double_quotes + EnforcedStyle: single_quotes VariableInterpolation: Enabled: false diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 0e3ab87a7..1d84b947d 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -48,14 +48,6 @@ def destroy redirect_to organizations_path, notice: "deleted" end - def give_time - @destination = @organization.account.id - @source = find_transfer_source - @offer = find_transfer_offer - @transfer = Transfer.new(source: @source, destination: @destination) - @sources = find_transfer_sources_for_admin - end - def set_current if current_user session[:current_organization_id] = @organization.id @@ -70,20 +62,4 @@ def organization_params public_opening_times description address neighborhood city domain]) end - - def find_transfer_offer - current_organization.offers. - find(params[:offer]) if params[:offer].present? - end - - def find_transfer_source - current_user.members. - find_by(organization: @organization).account.id - end - - def find_transfer_sources_for_admin - return unless admin? - [current_organization.account] + - current_organization.member_accounts.where("members.active is true") - end end diff --git a/app/controllers/transfers_controller.rb b/app/controllers/transfers_controller.rb index 8d4f4f432..29704e42b 100644 --- a/app/controllers/transfers_controller.rb +++ b/app/controllers/transfers_controller.rb @@ -6,12 +6,29 @@ def create transfer_params.merge(source: @source, destination: @account) ) - begin - transfer.save! - rescue ActiveRecord::RecordInvalid - flash[:error] = transfer.errors.full_messages.to_sentence - end + transfer.save! redirect_to redirect_target + rescue ActiveRecord::RecordInvalid + flash[:error] = transfer.errors.full_messages.to_sentence + end + + def new + transfer_factory = TransferFactory.new( + current_organization, + current_user, + params[:offer], + params[:destination_account_id] + ) + + render( + :new, + locals: { + accountable: transfer_factory.accountable, + transfer: transfer_factory.build_transfer, + offer: transfer_factory.offer, + sources: transfer_factory.transfer_sources + } + ) end def delete_reason diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 68f6fb828..89f2cd6ad 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -61,18 +61,6 @@ def update end end - def give_time - @user = scoped_users.find(params[:id]) - @destination = @user.members. - find_by(organization: current_organization).account.id - @source = find_transfer_source - @offer = find_transfer_offer - @transfer = Transfer.new(source: @source, - destination: @destination, - post: @offer) - @sources = find_transfer_sources_for_admin - end - private def user_params @@ -85,22 +73,6 @@ def user_params params.require(:user).permit *fields_to_permit end - def find_transfer_offer - current_organization.offers. - find(params[:offer]) if params[:offer].present? - end - - def find_transfer_source - current_user.members. - find_by(organization: current_organization).account.id - end - - def find_transfer_sources_for_admin - return unless admin? - [current_organization.account] + - current_organization.member_accounts.where("members.active is true") - end - def find_user if current_user.id == params[:id].to_i current_user diff --git a/app/helpers/transfers_helper.rb b/app/helpers/transfers_helper.rb index 98355baf5..1110e0e24 100644 --- a/app/helpers/transfers_helper.rb +++ b/app/helpers/transfers_helper.rb @@ -1,2 +1,9 @@ module TransfersHelper + def accountable_path(accountable) + if accountable.is_a?(Organization) + organization_path(accountable) + else + user_path(accountable.user) + end + end end diff --git a/app/models/member.rb b/app/models/member.rb index a9f996fa0..6c0b9aeca 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -29,6 +29,15 @@ def display_name_with_uid "#{user} (#{member_uid})" end + # Returns the id to be displayed in the :new transfer page + # + # @params _destination_accountable used to keep the same API as + # Organization#display_id + # @return [Integer] + def display_id(_destination_accountable) + member_uid + end + def remove_all_posts_from_index Post.with_member.where("members.id = ?", self.id).find_each do |post| post.delete_document diff --git a/app/models/organization.rb b/app/models/organization.rb index 7bf51d42c..47d81c789 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -28,6 +28,23 @@ def to_s "#{name}" end + def display_name_with_uid + self + end + + # Returns the id to be displayed in the :new transfer page with the given + # destination_accountable + # + # @params destination_accountable [Organization | Object] target of a transfer + # @return [Integer | String] + def display_id(destination_accountable) + if destination_accountable.is_a?(Organization) + account.accountable_id + else + '' + end + end + def ensure_reg_number_seq! update_column(:reg_number_seq, members.maximum(:member_uid)) end diff --git a/app/models/transfer_factory.rb b/app/models/transfer_factory.rb new file mode 100644 index 000000000..a093299cd --- /dev/null +++ b/app/models/transfer_factory.rb @@ -0,0 +1,80 @@ +class TransferFactory + def initialize(current_organization, current_user, offer_id, destination_account_id) + @current_organization = current_organization + @current_user = current_user + @offer_id = offer_id + @destination_account_id = destination_account_id + end + + # Returns the offer that is the subject of the transfer + # + # @return [Maybe] + def offer + current_organization.offers.find_by_id(offer_id) + end + + # Returns a new instance of Transfer with the data provided + # + # @return [Transfer] + def build_transfer + transfer = Transfer.new(source: source, destination: destination_account.id) + transfer.post = offer unless for_organization? + transfer + end + + def transfer_sources + if admin? + [current_organization.account] + + current_organization.member_accounts.merge(Member.active) + else + [] + end + end + + def accountable + @accountable ||= destination_account.accountable + end + + private + + attr_reader :current_organization, :current_user, :offer_id, + :destination_account_id + + # Returns the id of the account that acts as source of the transfer. + # Either the account of the organization or the account of the current user. + # + # @return [Maybe] + def source + organization = if accountable.is_a?(Organization) + accountable + else + current_organization + end + + current_user.members.find_by(organization: organization).account.id + end + + # Checks whether the destination account is an organization + # + # @return [Boolean] + def for_organization? + destination_account.accountable.class == Organization + end + + def admin? + current_user.try :manages?, current_organization + end + + # TODO: this method implements authorization by scoping the destination + # account in all the accounts of the current organization. If the specified + # destination account does not belong to it, the request will simply faily. + # + # Returns the account the time will be transfered to + # + # @return [Account] + def destination_account + @destination_account ||= current_organization + .all_accounts + .find(destination_account_id) + end +end diff --git a/app/models/transfer_sources_options.rb b/app/models/transfer_sources_options.rb new file mode 100644 index 000000000..9a3905a3a --- /dev/null +++ b/app/models/transfer_sources_options.rb @@ -0,0 +1,43 @@ +# Wraps the passed sources as a collection of HTML