Skip to content

Commit

Permalink
Move specific seeding action to separate function
Browse files Browse the repository at this point in the history
This (tries to )makes it easier to add something to the seeds without
breaking the existing specs. At the moment the specs expect the
`build` method of ProjectFactory to set things up in a particular
state.

If we set it up in that state we can't run an invoice simulation, so I
took that block out and put it in a different method that only gets
called while seeding
  • Loading branch information
pjaspers committed Dec 6, 2018
1 parent d6126c4 commit 68f58cb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/controllers/setup/seeds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def index
current_user.program.create_project_anchor unless current_user.program.project_anchor
project_anchor = current_user.program.project_anchor
project_factory = ProjectFactory.new
suffix = Time.now.to_s[0..15] + " - "
suffix = project_factory.normalized_suffix
project = project_factory.build(
dhis2_url: dhis2_url,
user: "admin",
Expand All @@ -12,7 +12,7 @@ def index
project_anchor: project_anchor
)
project_factory.update_links(project, suffix)

project_factory.additional_seed_actions(project, suffix)
project_anchor.projects.destroy_all
project_anchor.projects.push project

Expand Down
73 changes: 51 additions & 22 deletions app/services/project_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,6 @@ def build(project_props = { dhis2_url: "http://play.dhis2.org/demo", user: "admi
end

def update_links(project, suffix = "")
if (suffix || "") =~ /^[0-9]/
# Packages should not start with a number
suffix = "P#{suffix}"
end
project.build_entity_group(
name: "contracted entities",
external_reference: "external_reference"
Expand Down Expand Up @@ -294,16 +290,12 @@ def update_links(project, suffix = "")

claimed_state = project.states.find { |s| s.name == "Claimed" }
tarif_state = project.states.find { |s| s.name == "Tarif" }
verified_state = project.states.find { |s| s.name == "Verified" }
max_state = project.states.find { |s| s.name == "Max. Score" }

activity_1 = project.activities.build(
project: project,
name: "Vaccination", activity_states_attributes: [
{ name: "Vaccination claimed", state: claimed_state, external_reference: "cl-ext-1" },
{ name: "tarif for Vaccination ", state: tarif_state, external_reference: "tarif-ext-1" },
{ name: "Verified", state: verified_state, external_reference: 'verified-ext-1'},
{ name: "Max. Score", state: max_state, external_reference: 'max-score-ext-1'}
{ name: "tarif for Vaccination ", state: tarif_state, external_reference: "tarif-ext-1" }
]
)

Expand All @@ -320,9 +312,7 @@ def update_links(project, suffix = "")
name: "tarif for Clients sous traitement ARV suivi pendant les 6 premiers mois",
state: tarif_state,
external_reference: "tarif-ext-2"
},
{ name: "Verified", state: verified_state, external_reference: 'verified-ext-1'},
{ name: "Max. Score", state: max_state, external_reference: 'max-score-ext-1'}
}
]
)

Expand All @@ -335,10 +325,59 @@ def update_links(project, suffix = "")
content: fixture_content(:scorpio, "decision_table.csv")
)

refresh_packages!(project, suffix)
end

# These only get run by the seed controller. Adding them to the normal build operation would break some specs, since these two are not really related, keeping these separate.
def additional_seed_actions(project, suffix = "")
verified_state = project.states.find { |s| s.name == "Verified" }
max_state = project.states.find { |s| s.name == "Max. Score" }

activity_1 = project.activities.detect{|a| a.name == "Vaccination"}
activity_1.activity_states.build(
{ name: "Verified", state: verified_state, external_reference: 'M62VHgYT2n0'}
)
activity_1.activity_states.build(
{ name: "Max. Score", state: max_state, external_reference: 'FQ2o8UBlcrS'}
)

activity_2 = project.activities.last
activity_2.activity_states.build(
{ name: "Verified", state: verified_state, external_reference: 'CecywZWejT3'}
)
activity_2.activity_states.build(
{ name: "Max. Score", state: max_state, external_reference: 'bVkFujnp3F2'}
)
end

def normalized_suffix
suffix = Time.now.to_s[0..15] + " - "
if (suffix || "") =~ /^[0-9]/
# Packages should not start with a number
suffix = "P#{suffix}"
end
suffix
end

private

def states_in(project, state_names)
project.states.select { |s| state_names.include?(s.name) }
end

def fixture_content(type, name)
File.read(File.join("spec", "fixtures", type.to_s, name))
end

def refresh_packages!(project, suffix)
default_quantity_states = states_in(project, %w[Claimed Verified Tarif])
default_quality_states = states_in(project, ["Claimed", "Verified", "Max. Score"])
default_performance_states = states_in(project, ["Claimed", "Max. Score", "Budget"])

hospital_group = { name: "Hospital", organisation_unit_group_ext_ref: "tDZVQ1WtwpA" }
clinic_group = { name: "Clinic", organisation_unit_group_ext_ref: "RXL3lPSK8oG" }
admin_group = { name: "Administrative", organisation_unit_group_ext_ref: "w0gFTTmsUcF" }

# States have been created, now we can update them with actual
# identifiers from DHIS, these are taken from:
#
Expand Down Expand Up @@ -370,16 +409,6 @@ def update_links(project, suffix = "")
)
end

private

def states_in(project, state_names)
project.states.select { |s| state_names.include?(s.name) }
end

def fixture_content(type, name)
File.read(File.join("spec", "fixtures", type.to_s, name))
end

def update_package_with_dhis2(package, suffix, states, groups, activity_ids)
package.states = states
package.name = suffix + package.name
Expand Down

0 comments on commit 68f58cb

Please sign in to comment.