Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #505 from farismosman/master
Browse files Browse the repository at this point in the history
[#1668] Unable to create forms when Base language is in Arabic
  • Loading branch information
rdsubhas committed May 13, 2013
2 parents a4b7610 + d76624f commit 1e3ae5d
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
14 changes: 13 additions & 1 deletion app/models/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Field < Hash
localize_properties [:display_name, :help_text, :option_strings_text]
attr_reader :options
property :base_language, :default=>'en'

TEXT_FIELD = "text_field"
TEXT_AREA = "textarea"
RADIO_BUTTON = "radio_button"
Expand Down Expand Up @@ -58,9 +59,20 @@ class Field < Hash
validates_with_method :display_name, :method => :validate_unique_display_name
validates_with_method :option_strings, :method => :validate_has_2_options
validates_with_method :option_strings, :method => :validate_has_a_option
validates_format_of :display_name, :with => /([a-zA-Z]+)/, :message => I18n.t("activerecord.errors.models.field.display_name_format")
validates_with_method :display_name, :method => :validate_name_format
validates_with_method :display_name, :method => :valid_presence_of_base_language_name

def validate_name_format
special_characters = /[*!@#%$\^]/
white_spaces = /^(\s+)$/
if (display_name =~ special_characters) || (display_name =~ white_spaces)
errors.add(:display_name, I18n.t("activerecord.errors.models.field.display_name_format"))
return false
else
return true
end
end

def valid_presence_of_base_language_name
if base_language==nil
self.base_language='en'
Expand Down
14 changes: 13 additions & 1 deletion app/models/form_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FormSection < CouchRestRails::Document
view_by :order
validates_presence_of "name_#{I18n.default_locale}", :message => I18n.t("activerecord.errors.models.form_section.presence_of_name")
validates_with_method :name, :method => :valid_presence_of_base_language_name
validates_format_of :name, :with => /^([a-zA-Z0-9_\s]*)$/, :message => I18n.t("activerecord.errors.models.form_section.format_of_name")
validates_with_method :name, :method => :validate_name_format
validates_with_method :unique_id, :method => :validate_unique_id
validates_with_method :name, :method => :validate_unique_name
validates_with_method :visible, :method => :validate_visible_field, :message => I18n.t("activerecord.errors.models.form_section.visible_method")
Expand Down Expand Up @@ -85,6 +85,7 @@ def self.get_by_unique_id unique_id

def self.add_field_to_formsection formsection, field
raise I18n.t("activerecord.errors.models.form_section.add_field_to_form_section") unless formsection.editable
field.merge!({'base_language' => formsection['base_language']})
formsection.fields.push(field)
formsection.save
end
Expand Down Expand Up @@ -180,6 +181,17 @@ def order_fields new_field_names

protected

def validate_name_format
special_characters = /[*!@#%$\^]/
white_spaces = /^(\s+)$/
if (name =~ special_characters) || (name =~ white_spaces)
errors.add(:name, I18n.t("activerecord.errors.models.form_section.format_of_name"))
return false
else
return true
end
end

def validate_visible_field
self.visible = true if self.perm_visible?
true
Expand Down
2 changes: 1 addition & 1 deletion app/models/search_criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def self.lucene_query(criteria_list)
def to_lucene_query
phrases = value.split(/\s+OR\s+/i)
phrases.map do |phrase|
query = phrase.split(/[ ,]+/).map {|word| "(#{field}_text:#{word.downcase}~ OR #{field}_text:#{word.downcase}*)"}.join(" AND ")
query = phrase.split(/[ ,-]+/).map {|word| "(#{field}_text:#{word.downcase}~ OR #{field}_text:#{word.downcase}*)"}.join(" AND ")
"(#{query})"
end.join(" OR ")
end
Expand Down
2 changes: 1 addition & 1 deletion capybara_features/create_new_form.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Feature: Create new forms

Given I am on form section page
And I follow "Create New Form Section"
And I fill in "form_section_name" with "This is D£dgy"
And I fill in "form_section_name" with "This is D$dgy"
And I fill in "form_section_description" with "I am a new custom form. Say hello!"

When I press "Save Details"
Expand Down
2 changes: 1 addition & 1 deletion capybara_features/upload_a_childs_audio.feature
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Feature:
Then I should see "Child was successfully updated"
And I click the "Photos and Audio" link
And I should see an audio element that can play the audio file named "sample.mp3"
And the record history should log "Audio changed "
And the record history should log "Audio changed"
And the record history should log "by bob"

@gc
Expand Down
2 changes: 1 addition & 1 deletion public/javascripts/translations.js

Large diffs are not rendered by default.

39 changes: 29 additions & 10 deletions spec/models/form_section_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

describe FormSection do

def mock_formsection(stubs={})
stubs.reverse_merge!(:fields=>[], :save => true, :editable => true)
@mock_formsection ||= mock_model(FormSection, stubs)
def create_formsection(stubs={})
stubs.reverse_merge!(:fields=>[], :save => true, :editable => true, :base_language => "en")
@create_formsection = FormSection.new stubs
end

def new_field(fields = {})
Expand Down Expand Up @@ -123,15 +123,22 @@ def new_should_be_called_with (name, value)

it "adds the field to the formsection" do
field = Field.new_text_field("name")
formsection = mock_formsection :fields => [new_field(), new_field()], :save=>true
formsection = create_formsection :fields => [new_field(), new_field()], :save => true
FormSection.add_field_to_formsection formsection, field
formsection.fields.length.should == 3
formsection.fields[2].should == field
end

it "adds base_language to fields in formsection" do
field = Field.new_textarea("name")
formsection = create_formsection :fields => [new_field(), new_field()], :save=>true
FormSection.add_field_to_formsection formsection, field
formsection.fields[2].should have_key("base_language")
end

it "saves the formsection" do
field = Field.new_text_field("name")
formsection = mock_formsection
formsection = create_formsection
formsection.should_receive(:save)
FormSection.add_field_to_formsection formsection, field
end
Expand All @@ -149,15 +156,15 @@ def new_should_be_called_with (name, value)

it "adds the textarea to the formsection" do
field = Field.new_textarea("name")
formsection = mock_formsection :fields => [new_field(), new_field()], :save=>true
formsection = create_formsection :fields => [new_field(), new_field()], :save=>true
FormSection.add_field_to_formsection formsection, field
formsection.fields.length.should == 3
formsection.fields[2].should == field
end

it "saves the formsection with textarea field" do
field = Field.new_textarea("name")
formsection = mock_formsection
formsection = create_formsection
formsection.should_receive(:save)
FormSection.add_field_to_formsection formsection, field
end
Expand All @@ -168,15 +175,15 @@ def new_should_be_called_with (name, value)

it "adds the select drop down to the formsection" do
field = Field.new_select_box("name", ["some", ""])
formsection = mock_formsection :fields => [new_field(), new_field()], :save=>true
formsection = create_formsection :fields => [new_field(), new_field()], :save=>true
FormSection.add_field_to_formsection formsection, field
formsection.fields.length.should == 3
formsection.fields[2].should == field
end

it "saves the formsection with select drop down field" do
field = Field.new_select_box("name", ["some", ""])
formsection = mock_formsection
formsection = create_formsection
formsection.should_receive(:save)
FormSection.add_field_to_formsection formsection, field
end
Expand Down Expand Up @@ -292,11 +299,23 @@ def new_should_be_called_with (name, value)
end

it "should validate name is alpha_num" do
form_section = FormSection.new(:name=>"££ss")
form_section = FormSection.new(:name=> "r@ndom name!")
form_section.should_not be_valid
form_section.errors.on(:name).should be_present
end

it "should not allow name with white speces only" do
form_section = FormSection.new(:name=> " ")
form_section.should_not be_valid
form_section.errors.on(:name).should be_present
end

it "should allow arabic names" do
form_section = FormSection.new(:name=>"العربية")
form_section.should be_valid
form_section.errors.on(:name).should_not be_present
end

it "should validate name is unique" do
same_name = 'Same Name'
valid_attributes = {:name => same_name, :unique_id => same_name.dehumanize, :description => '', :visible => true, :order => 0}
Expand Down

0 comments on commit 1e3ae5d

Please sign in to comment.