Skip to content

Instantly share code, notes, and snippets.

@JFickel
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFickel/8747048 to your computer and use it in GitHub Desktop.
Save JFickel/8747048 to your computer and use it in GitHub Desktop.
require 'spec_helper'
feature 'delphius user creates a chart of accounts' do
scenario "successfully", js: true do
sign_in_as_delphius_user
chart_of_accounts = new_chart_of_accounts
chart_of_accounts.create
chart_of_accounts.has_been_created
end
def new_chart_of_accounts
entity = create(:entity)
ChartOfAccountsOnPage.new(entity)
end
def sign_in_as_delphius_user
delphius = create(:delphius)
set_host "#{delphius.entity.subdomain}.lvh.me", 31337
log_in delphius
end
class ChartOfAccountsOnPage
include Capybara::DSL
include Rails.application.routes.url_helpers
def initialize(entity, options={})
@entity = entity
@filename = options[:filename] || 'bishop_coa.xlsx'
end
def create
visit entity_path(entity)
click_link 'Manage Data'
click_link 'New Chart of Accounts'
# has_content?("Upload a Chart of Accounts File")
# puts page.body
save_and_open_page
attach_file 'file_uploader_file[attached]', Rails.root.join('spec', 'fixtures', filename)
click_button 'Upload'
fill_in 'Name', with: 'Bishop CoA'
set_tree_columns
click_button 'Process Chart of Accounts'
end
def set_tree_columns
set_funds_columns
set_departments_columns
set_account_types_columns
end
def set_funds_columns
find(:css, "#classify_inputs div:nth-child(1) .column_cell:nth-child(2) .column_input").set("D")
find(:css, "#classify_inputs div:nth-child(1) :nth-child(4) .column_input").set("C")
find(:css, "#classify_inputs div:nth-child(1) :nth-child(6) .column_input").set("B")
find(:css, "#classify_inputs div:nth-child(1) :nth-child(8) .column_input").set("A")
end
def set_departments_columns
find(:css, "#classify_inputs div:nth-child(2) .classify_data_wrapper_outer .column_cell:nth-child(2) .column_input").set("G")
find(:css, "#classify_inputs div:nth-child(2) .classify_data_wrapper_outer :nth-child(4) .column_input").set("F")
end
def set_account_types_columns
find(:css, "div:nth-child(3) .column_cell:nth-child(2) .column_input").set("M")
find(:css, "div:nth-child(3) :nth-child(4) .column_input").set("K")
find(:css, "div:nth-child(3) :nth-child(6) .column_input").set("J")
find(:css, "div:nth-child(3) :nth-child(8) .column_input").set("I")
end
def has_been_created
has_content?("Successfully processed file!") # && has_css?('[data-role=chart]', text: filename)
end
private # uniform access principle
attr_reader :entity, :filename
end
scenario "unsuccessfully" do
pending
end
end
class FileUploader::FilesController < ApplicationController
before_filter :set_associated_model_names, :set_classification, :set_entity, only: [:new, :create]
before_filter :new_file_uploader_file, only: [:create]
load_and_authorize_resource :file_uploader_file, class: FileUploader::File, parent: false
skip_authorize_resource :file_uploader_file, only: :new
# GET /file_uploader/files/new
def new
end
# POST /file_uploader/files
def create
respond_to do |format|
if @file_uploader_file.save
params = { id: @file_uploader_file.id, entity: @file_uploader_file.entity.id }
format.html { redirect_to next_page(@file_uploader_file.classification, params) }
else
format.html { render action: "new" }
end
end
end
# PUT /file_uploader/files/1
def update
respond_to do |format|
if @file_uploader_file.update_attributes(file_uploader_file_params)
format.html { redirect_to @file_uploader_file, notice: 'File was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
end
end
end
# GET /file_uploader/files/1/import_settings/edit
def edit_import_settings
begin
@preview = @file_uploader_file.preview
@file_name = @file_uploader_file.attached_file_name
gon.push import_settings: JSON.parse(@file_uploader_file.import_settings)
@entity_name = @file_uploader_file.entity.name
gon.push parse_url: file_uploader_parse_path(@file_uploader_file.id, create: true, :format => :json)
rescue Exception => e
respond_to do |format|
@classification = @file_uploader_file.file_classification
@entity = @file_uploader_file.entity
flash[:error] = e.message;
format.html {redirect_to :back}
end
end
end
def edit_import_settings_coa
edit_import_settings
end
def edit_import_settings_ledger
import_settings = JSON.parse(@file_uploader_file.import_settings)
@coa_name = DataManager::ChartOfAccounts.find(import_settings['coa_id']).name if import_settings['coa_id']
edit_import_settings
end
# DELETE /file_uploader/files/1
def destroy
@file_uploader_file.destroy
respond_to do |format|
format.html { redirect_to next_page(@file_uploader_file.classification, entity: @file_uploader_file.entity.id) }
end
end
def parse
@file_uploader_file.import_settings = params.to_json
@file_uploader_file.save
result, error = @file_uploader_file.parse(create: params[:create])
if error #the request is invalid -- didn't send to FPAPI
render status: error, json: result
else
render status: 200, json: {
bundle_url: data_manager_bundles_path(coa: result[:coa]),
coa_url: edit_data_manager_chart_of_account_path(id: result[:coa])
}
end
end
private
def next_page(classification, params = {})
case classification
when 'coa'
file_uploader_edit_import_settings_coa_path(params)
when 'ledger'
file_uploader_edit_import_settings_ledger_path(params)
when 'budget', 'annotation'
entity_path({ id: params[:entity] })
end
end
def set_classification
@classification = FileUploader::FileClassification.find_by_classification(params[:classification])
end
def set_entity
@entity = Entity.find(params[:entity])
end
def set_associated_model_names
coa_id = params[:coa]
if coa_id
@coa_name = DataManager::ChartOfAccounts.find(coa_id).name
end
end
# Prevents load_and_authorize from calling new(params) since strong_parameters will bark at that.
def new_file_uploader_file
@file_uploader_file = FileUploader::File.new(file_uploader_file_params)
end
def file_uploader_file_params
params.require(:file_uploader_file).permit(:entity_id, :file_classification_id, :attached, :import_settings)
end
end
<%= render_breadcrumbs :builder => ::FoundationBreadcrumbsBuilder %>
<section class="panel">
<header class="panel__header">
<h3 class="panel__header__title">
Upload a <%= @classification.name %> File
<span>
<%= "&ndash; #{@entity.name}".html_safe if @entity %> <% if @coa_name %>&ndash;<%= @coa_name %> Chart of Accounts <% end %>
</span>
</h3>
</header>
<%= render 'form' %>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment