Skip to content

Commit

Permalink
Mapping Status| Added mapping_status to survey. Determined mapping st…
Browse files Browse the repository at this point in the history
…atus of exisitng surveys. Also on each mapping update, system will determine if the status needs to be changed. Updated views to display mapping status on surveys & surveys>mapping screens. Added list of questions not yet mapped to the mapping screen. On the surveys>mappings screen user can update mapping status
  • Loading branch information
kumar-amit committed Jan 17, 2013
1 parent 6f1a56e commit 22aea3d
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/controllers/field_mappings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def new
@object_mapping.build_unmapped_field_mappings
@questions = @object_mapping.survey.questions
@questions_for_select = @questions.map {|question| [question.name, question.name]}
@unmapped_questions = @object_mapping.survey.unmapped_questions.collect(&:name)
@salesforce_objects = Salesforce::Base.where(:enabled => true)
add_crumb 'Surveys', surveys_path
add_crumb 'Mappings', survey_mappings_path(@object_mapping.survey_id)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/object_mappings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def create
def update
@object_mapping = ObjectMapping.find(params[:id])
if @object_mapping.update_attributes(sanitized_params)
@object_mapping.survey.update_mapping_status
flash[:notice] = "Successfully saved the mappings."
else
flash[:error] = "The mapping was not saved. Please check log file for more."
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/surveys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ def search
add_crumb "Search results for: #{params[:query]}"
render :index
end

def update_mapping_status
survey = EpiSurveyor::Survey.find params[:id]
survey.update_attributes!(:mapping_status => params[:mapping_status])
flash[:notice] = "Mapping status updated"
redirect_to survey_mappings_path(survey)
end

private
def errors_count import_histories
Expand Down
23 changes: 21 additions & 2 deletions app/models/epi_surveyor/survey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Survey < ActiveRecord::Base
has_many :object_mappings, :dependent => :destroy
has_many :import_histories, :dependent => :destroy

attr_accessible :id, :notification_email
attr_accessible :id, :notification_email, :mapping_status
attr_accessor :responses

scope :ordered, order('name ASC')
Expand All @@ -19,6 +19,13 @@ class Survey < ActiveRecord::Base

scope :starting_with, lambda { |starting_char| where("UPPER(surveys.name) like ?", starting_char + '%') }

module MAPPING_STATUS
MAPPED = 'Mapped'
PARTIAL = 'Partial'
UNMAPPED = 'Unmapped'
ALL = [MAPPED, PARTIAL, UNMAPPED]
end

def find_potential_list_of_target_surveys_for_cloning_mappings_into
Survey.all.reject{|survey| survey == self}.sort_by(&:name)
end
Expand Down Expand Up @@ -131,7 +138,19 @@ def self.delete_old_surveys mapping_last_modified_at
survey.destroy
end

end
end

def update_mapping_status
update_attributes!(:mapping_status => ((unmapped_questions.count == questions.count) ? MAPPING_STATUS::UNMAPPED : MAPPING_STATUS::PARTIAL)) if mapping_status != MAPPING_STATUS::MAPPED
end

def unmapped_questions
return questions unless object_mappings.present?
questions.reject do |question|
filed_mappings = object_mappings.collect(&:field_mappings).flatten
filed_mappings.collect(&:question_name).index{ |x| x =~/#{question.name}/ }.present? || filed_mappings.collect(&:lookup_condition).index{ |x| x =~/<#{question.name}>/ }.present?
end
end

end

Expand Down
4 changes: 4 additions & 0 deletions app/views/field_mappings/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@

<%= render :partial => 'help' %>

<div class="unmapped_fields">
<b>Unmapped Questions:</b> <%= select_tag("unmapped_questions", options_for_select(@unmapped_questions)) %>
</div>

<%= form_for @object_mapping do |object_mapping_form| %>
<div class="field_mapping_form">
<div class="field_mappings">
Expand Down
8 changes: 8 additions & 0 deletions app/views/mappings/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

<%= render :partial => 'edit_links' %>

<div class="mapping_details">
<%= form_tag update_mapping_status_survey_path(@survey), :method => :post do %>
<b>Mapping Status:</b> <span class="<%= @survey.mapping_status.underscore %>"><%= @survey.mapping_status %></span> |
<b>Update Status:</b> <%= select_tag("mapping_status", options_for_select(EpiSurveyor::Survey::MAPPING_STATUS::ALL, EpiSurveyor::Survey::MAPPING_STATUS::MAPPED)) %>
<%= submit_tag 'Update' %>
<% end %>
</div>

<ol class="object_mappings">
<% @survey.object_mappings.each do |object_mapping|%>
<li class="object_mapping">
Expand Down
7 changes: 4 additions & 3 deletions app/views/surveys/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@
<div class="survey">
<div class="survey_name">
<%= check_box_tag 'survey_ids[]', survey.id %>
<span><%= survey.name %></span> |
<%= "Imported #{distance_of_time_in_words(Time.now, survey.last_imported_at)} ago" if survey.last_imported_at%> |
<span><%= survey.name %></span> |
<%= "Imported #{distance_of_time_in_words(Time.now, survey.last_imported_at)} ago" if survey.last_imported_at%> |
<span class="survey_last_modified"><%= "Mapping changed #{distance_of_time_in_words(Time.now, survey.mapping_last_modified_at)} ago" if survey.mapping_last_modified_at%></span>
</div>
<div class="actions">
<div class="actions">
<span class="<%= survey.mapping_status.underscore %>"><%= survey.mapping_status %></span>|
<%= link_to 'Mappings', survey_mappings_path(survey.id) %> |
<%= link_to 'History', survey_import_histories_path(survey.id) %> |
<%= link_to 'Questions', survey_questions_path(survey.id) %> |
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
member do
get 'edit'
put 'update'
post 'update_mapping_status'
end

resources :import_histories do
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20130114124504_add_mapping_status_to_surveys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AddMappingStatusToSurveys < ActiveRecord::Migration
def self.up
add_column :surveys, :mapping_status, :string, :default => ''

EpiSurveyor::Survey.all.each do |survey|
survey.update_mapping_status
end

end

def self.down
remove_column :surveys, :mapping_status
end
end

3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20121102085123) do
ActiveRecord::Schema.define(:version => 20130114124504) do

create_table "add_default_value_to_field_mapppings", :force => true do |t|
t.datetime "created_at"
Expand Down Expand Up @@ -80,6 +80,7 @@
t.datetime "updated_at"
t.string "notification_email"
t.datetime "mapping_last_modified_at"
t.string "mapping_status", :default => ""
end

create_table "sync_errors", :force => true do |t|
Expand Down
20 changes: 20 additions & 0 deletions public/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,24 @@ span.survey_last_modified{
width: 25px;
height: 25px;
text-indent: -9999px;
}

div.mapping_details{
padding-top: 15px;
}

div.unmapped_fields{
padding-top: 15px;
}

span.unmapped{
color: red;
}

span.mapped{
color: green;
}

span.partial{
color: blue;
}
2 changes: 2 additions & 0 deletions spec/controllers/field_mappings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
it 'should populate object mapping and questions' do
ObjectMapping.should_receive(:find).with(2).and_return object_mapping
object_mapping.should_receive :build_unmapped_field_mappings
survey.should_receive(:unmapped_questions).and_return [question]

get :new, :object_mapping_id => 2

response.should be_success
assigns[:object_mapping].should == object_mapping
assigns[:questions].should == [question]
assigns[:questions_for_select].should == [['name', 'name']]
assigns[:unmapped_questions].should == ['name']
end
end

Expand Down
5 changes: 4 additions & 1 deletion spec/controllers/object_mappings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@
}
}
}

survey = EpiSurveyor::Survey.new(:id => 1)
EpiSurveyor::Survey.stub(:find).with(1, anything).and_return(survey)
survey.should_receive(:update_mapping_status)
mapping = ObjectMapping.create(:survey_id => 1, :salesforce_object_name => 'AnObject')

put :update, :id => mapping.id, :object_mapping => params[:object_mapping]
mapping.reload
mapping.field_mappings.first.field_name.should == 'a_field'
Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/surveys_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@
assigns[:surveys].should == []
end
end

describe 'update_mapping_status' do
it 'should update mapping_status' do
survey = EpiSurveyor::Survey.create(:id => 1)
post 'update_mapping_status', :id => 1, :mapping_status => 'Mapped'
survey.reload
survey.mapping_status.should == 'Mapped'
response.should redirect_to survey_mappings_path(survey)
end
end
end
75 changes: 75 additions & 0 deletions spec/models/epi_surveyor/survey_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,80 @@
EpiSurveyor::Survey.count.should == 1
end
end

describe "unmapped_questions" do
it "should return all questions list if object_mapping is not present" do
survey = EpiSurveyor::Survey.new
questions = [EpiSurveyor::Question.new, EpiSurveyor::Question.new]
survey.should_receive(:questions).and_return(questions)

survey.unmapped_questions.should == questions
end

it "should exclude questions which are mapped directly as questions" do
survey = EpiSurveyor::Survey.new
question1 = EpiSurveyor::Question.new
question1.name = 'question1'
question2 = EpiSurveyor::Question.new
question2.name = 'question2'
survey.object_mappings << ObjectMapping.new
survey.object_mappings.first.field_mappings << FieldMapping.new(:question_name => 'question2')
survey.should_receive(:questions).and_return([question1, question2])

result = survey.unmapped_questions

result.size.should == 1
result.first.name.should == 'question1'
end

it "should exclude questions which are mapped through lookup_condition" do
survey = EpiSurveyor::Survey.new
question1 = EpiSurveyor::Question.new
question1.name = 'question1'
question2 = EpiSurveyor::Question.new
question2.name = 'question2'
survey.object_mappings << ObjectMapping.new
survey.object_mappings.first.field_mappings << FieldMapping.new(:lookup_condition => "School WHERE District__c='a1AT0000000x5UL' AND Name=<question2>")
survey.should_receive(:questions).and_return([question1, question2])

result = survey.unmapped_questions

result.size.should == 1
result.first.name.should == 'question1'
end
end

describe "update_mapping_status" do
it "should update mapping status as Unmapped if all questions are unmapped" do
survey = EpiSurveyor::Survey.create
questions = [EpiSurveyor::Question.new, EpiSurveyor::Question.new]
survey.should_receive(:questions).and_return(questions)
survey.should_receive(:unmapped_questions).and_return(questions)

survey.update_mapping_status
survey.reload
survey.mapping_status.should == EpiSurveyor::Survey::MAPPING_STATUS::UNMAPPED
end

it "should update mapping status as Unmapped if some questions are mapped" do
survey = EpiSurveyor::Survey.create
question1 = EpiSurveyor::Question.new
question2 = EpiSurveyor::Question.new
survey.should_receive(:questions).and_return([question1, question2])
survey.should_receive(:unmapped_questions).and_return([question2])

survey.update_mapping_status
survey.reload
survey.mapping_status.should == EpiSurveyor::Survey::MAPPING_STATUS::PARTIAL
end

it "should update not update mapping status if mapping_status is Mapped" do
survey = EpiSurveyor::Survey.create(:mapping_status => EpiSurveyor::Survey::MAPPING_STATUS::MAPPED)

survey.update_mapping_status
survey.reload
survey.mapping_status.should == EpiSurveyor::Survey::MAPPING_STATUS::MAPPED
end
end

end

0 comments on commit 22aea3d

Please sign in to comment.