/spec/content_spec_helper.rb

https://github.com/jjuglans/Webiva · Ruby · 100 lines · 76 code · 22 blank · 2 comment · 8 complexity · 1e49e39749c03e39b4d09d719d1adac4 MD5 · raw file

  1. module ContentSpecHelper
  2. def connect_to_migrator_database
  3. # Switch to migrator
  4. @defaults_config_file = YAML.load_file("#{Rails.root}/config/defaults.yml")
  5. dmn = Domain.find(@defaults_config_file['testing_domain']).get_info
  6. DomainModel.activate_database(dmn,'migrator',false)
  7. # Kill the spec test table if no-go
  8. end
  9. def create_spec_test_content_model(args={ })
  10. ContentModel.connection.execute('DROP TABLE IF EXISTS cms_spec_tests')
  11. cm = ContentModel.create({:name => 'spec_test'}.merge(args))
  12. cm.create_table # Create the table
  13. cm
  14. end
  15. def create_dummy_fields(cm,create_fields = [ :string ] )
  16. fields = Content::CoreField.fields
  17. cmfs = []
  18. field_opts = { :options => { :options => "one;;a\ntwo;;b" },
  19. :multi_select => { :options => "option 1;;a\noption 2;;b\noption 3;;c" }
  20. }
  21. if !create_fields
  22. fields.each_with_index do |fld,idx|
  23. cmfs << ContentModelField.new(:name => "#{fld[:name]} Field",:field_type => fld[:name], :field_module => 'content/core_field', :position => idx+1,
  24. :field_options => field_opts[fld[:name]] || {} ).attributes
  25. end
  26. else
  27. create_fields.each_with_index do |fld_name,idx|
  28. fld = fields.detect { |fld_info| fld_info[:name] == fld_name.to_sym }
  29. if !create_fields || create_fields.include?(fld[:name])
  30. cmfs << ContentModelField.new(:name => "#{fld[:name]} Field",:field_type => fld[:name], :field_module => 'content/core_field', :position => idx+1,
  31. :field_options => field_opts[fld[:name]] || {} ).attributes
  32. end
  33. end
  34. end
  35. cm.update_table(cmfs)
  36. cm.reload
  37. cm
  38. end
  39. def create_content_model_with_all_fields(opts={})
  40. ContentModel.connection.execute('DROP TABLE IF EXISTS cms_controller_spec_tests')
  41. @cm = ContentModel.create({:name => 'controller_spec_test', :show_on_content => true}.merge(opts))
  42. @cm.create_table # Create the table
  43. fields = Content::CoreField.fields
  44. cmfs = []
  45. field_opts = {
  46. :options => { :options => "one;;a\ntwo;;b" },
  47. :multi_select => { :options => "option 1;;a\noption 2;;b\noption 3;;c" }
  48. }
  49. fields.each do |fld|
  50. cmfs << ContentModelField.new(:name => "#{fld[:name]} Field",
  51. :field_type => fld[:name],
  52. :field_module => 'content/core_field',
  53. :field_options => field_opts[fld[:name]] || {}
  54. ).attributes
  55. end
  56. @cm.update_table(cmfs)
  57. @cm.reload
  58. end
  59. def self.setup_content_model_test_with_all_fields(spec)
  60. spec.before(:all) do
  61. create_content_model_with_all_fields
  62. %w(content_models content_model_fields).each do |table|
  63. DomainModel.__skip_table table
  64. end
  65. end
  66. spec.after(:all) do
  67. DomainModel.__clear_skip_table
  68. %w(content_models content_model_fields).each do |table|
  69. DomainModel.__add_modified_table table
  70. end
  71. DomainModel.__truncate_modified_tables
  72. ContentModel.connection.execute('DROP TABLE IF EXISTS cms_controller_spec_tests')
  73. end
  74. spec.before(:each) do
  75. @cm = ContentModel.find @cm.id
  76. end
  77. end
  78. end