sugarcrm 0.9.11 → 0.9.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. data/README.rdoc +11 -0
  2. data/Rakefile +1 -0
  3. data/VERSION +1 -1
  4. data/lib/rails/generators/sugarcrm/config/config_generator.rb +22 -0
  5. data/lib/rails/generators/sugarcrm/config/templates/initializer.rb +4 -0
  6. data/lib/rails/generators/sugarcrm/config/templates/sugarcrm.yml +19 -0
  7. data/lib/sugarcrm.rb +1 -0
  8. data/lib/sugarcrm/associations/association_collection.rb +7 -0
  9. data/lib/sugarcrm/associations/associations.rb +5 -0
  10. data/lib/sugarcrm/attributes/attribute_methods.rb +17 -0
  11. data/lib/sugarcrm/base.rb +1 -1
  12. data/lib/sugarcrm/connection/api/get_available_modules.rb +1 -1
  13. data/lib/sugarcrm/connection/api/get_document_revision.rb +2 -2
  14. data/lib/sugarcrm/connection/api/get_entries.rb +1 -1
  15. data/lib/sugarcrm/connection/api/get_entries_count.rb +1 -1
  16. data/lib/sugarcrm/connection/api/get_entry.rb +1 -1
  17. data/lib/sugarcrm/connection/api/get_entry_list.rb +1 -1
  18. data/lib/sugarcrm/connection/api/get_module_fields.rb +1 -1
  19. data/lib/sugarcrm/connection/api/get_note_attachment.rb +1 -1
  20. data/lib/sugarcrm/connection/api/get_relationships.rb +2 -2
  21. data/lib/sugarcrm/connection/api/get_report_entries.rb +1 -1
  22. data/lib/sugarcrm/connection/api/get_user_id.rb +1 -1
  23. data/lib/sugarcrm/connection/api/get_user_team_id.rb +1 -1
  24. data/lib/sugarcrm/connection/api/logout.rb +1 -1
  25. data/lib/sugarcrm/connection/api/seamless_login.rb +1 -1
  26. data/lib/sugarcrm/connection/api/search_by_module.rb +1 -1
  27. data/lib/sugarcrm/connection/api/set_campaign_merge.rb +1 -1
  28. data/lib/sugarcrm/connection/api/set_document_revision.rb +18 -4
  29. data/lib/sugarcrm/connection/api/set_entries.rb +1 -1
  30. data/lib/sugarcrm/connection/api/set_entry.rb +1 -1
  31. data/lib/sugarcrm/connection/api/set_note_attachment.rb +1 -1
  32. data/lib/sugarcrm/connection/api/set_relationship.rb +1 -1
  33. data/lib/sugarcrm/connection/api/set_relationships.rb +1 -1
  34. data/lib/sugarcrm/connection/connection.rb +9 -5
  35. data/lib/sugarcrm/connection_pool.rb +162 -0
  36. data/lib/sugarcrm/exceptions.rb +1 -0
  37. data/lib/sugarcrm/module_methods.rb +3 -3
  38. data/lib/sugarcrm/session.rb +63 -28
  39. data/sugarcrm.gemspec +143 -0
  40. data/sugarcrm.tmproj +952 -0
  41. data/test/connection/test_login.rb +1 -2
  42. data/test/connection/test_set_document_revision.rb +28 -0
  43. data/test/helper.rb +1 -1
  44. data/test/test_connection_pool.rb +33 -0
  45. data/test/test_session.rb +33 -16
  46. data/test/test_sugarcrm.rb +2 -5
  47. metadata +71 -61
  48. data/.document +0 -5
  49. data/test/config_test.yaml +0 -15
data/README.rdoc CHANGED
@@ -140,6 +140,17 @@ A less clunky way to interact with SugarCRM via REST.
140
140
  }
141
141
  )
142
142
 
143
+ == USING THE GEM WITH RAILS 3
144
+
145
+ Note: this gem requires Rails 3 as it depends on Active Support >= 3.
146
+
147
+ 1. Add the sugarcrm gem to your Gemfile (sugarcrm gem version >= 0.9.12)
148
+ 2. Run `bundle install`
149
+ 4. Run `rails g sugarcrm:config`
150
+ 5. Edit the configuration file in `config/sugarcrm.yml` to match your environment
151
+
152
+ Example app here: https://github.com/davidsulc/sugar_on_rails_basic
153
+
143
154
  == USING A CONFIGURATION FILE
144
155
 
145
156
  If you want to use a configuration file instead of always specifying the url, username, and password to connect to SugarCRM, you can add your credentials to one (or more) of
data/Rakefile CHANGED
@@ -18,6 +18,7 @@ Jeweler::Tasks.new do |gem|
18
18
  gem.homepage = "http://github.com/chicks/sugarcrm"
19
19
  gem.authors = ["Carl Hicks", "David Sulc"]
20
20
  gem.executables = ['sugarcrm']
21
+ gem.files = FileList['./lib/**/*', './*'].exclude("*.lock").to_a
21
22
  end
22
23
  Jeweler::RubygemsDotOrgTasks.new
23
24
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.11
1
+ 0.9.12
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ module Sugarcrm
4
+ module Generators
5
+ class ConfigGenerator < Rails::Generators::Base
6
+ desc 'Creates a SugarCRM gem configuration file at config/sugarcrm.yml, and an initializer at config/initializers/sugarcrm.rb'
7
+
8
+ def self.source_root
9
+ @_sugarcrm_source_root ||= File.expand_path("../templates", __FILE__)
10
+ end
11
+
12
+ def create_config_file
13
+ template 'sugarcrm.yml', File.join('config', 'sugarcrm.yml')
14
+ end
15
+
16
+ def create_initializer_file
17
+ template 'initializer.rb', File.join('config', 'initializers', 'sugarcrm.rb')
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,4 @@
1
+ # Load the values in config/sugarcrm.yml into a hash
2
+ config_values = SugarCRM::Session.parse_config_file(File.join(Rails.root, 'config', 'sugarcrm.yml'))
3
+ # Connect to appropriate SugarCRM instance (depending on Rails environment)
4
+ SugarCRM::Session.from_hash(config_values[Rails.env.to_sym])
@@ -0,0 +1,19 @@
1
+ development:
2
+ base_url: http://127.0.0.1/sugarcrm # where your SugarCRM instance is located
3
+ username: admin
4
+ password: letmein
5
+ # connection pool settings
6
+ # if you don't know what purpose this serves, you can safely ignore these settings
7
+ # works similarly to Rails' database connection pooling (documented here: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionPool.html)
8
+ # pool: 3 # the maximum number of simulatenous connections the gem should open with the SugarCRM server (default is 5)
9
+ # wait_timeout: 7 # maximum time to wait for an available connection to SugarCRM server, in seconds (default is 5)
10
+
11
+ test:
12
+ base_url: http://127.0.0.1/sugarcrm
13
+ username: admin
14
+ password: letmein
15
+
16
+ production:
17
+ base_url: http://127.0.0.1/sugarcrm
18
+ username: admin
19
+ password: letmein
data/lib/sugarcrm.rb CHANGED
@@ -10,6 +10,7 @@ require 'json'
10
10
 
11
11
  require 'sugarcrm/session'
12
12
  require 'sugarcrm/module_methods'
13
+ require 'sugarcrm/connection_pool'
13
14
  require 'sugarcrm/connection'
14
15
  require 'sugarcrm/exceptions'
15
16
  require 'sugarcrm/finders'
@@ -81,6 +81,13 @@ module SugarCRM
81
81
  @collection.send(method_name.to_sym, *args, &block)
82
82
  end
83
83
 
84
+ # respond correctly for delegated methods
85
+ def respond_to?(method_name)
86
+ load
87
+ return true if @collection.respond_to? method_name
88
+ super
89
+ end
90
+
84
91
  def save
85
92
  begin
86
93
  save!
@@ -52,5 +52,10 @@ module SugarCRM
52
52
  @associations.send(method_name.to_sym, *args, &block)
53
53
  end
54
54
 
55
+ # respond correctly for delegated methods
56
+ def respond_to?(method_name)
57
+ return true if @associations.respond_to? method_name
58
+ super
59
+ end
55
60
  end
56
61
  end
@@ -54,6 +54,10 @@ module SugarCRM; module AttributeMethods
54
54
  false
55
55
  end
56
56
 
57
+ def destroyed?
58
+ @attributes[:deleted]
59
+ end
60
+
57
61
  def attributes_changed?
58
62
  @modified_attributes.length > 0
59
63
  end
@@ -62,11 +66,17 @@ module SugarCRM; module AttributeMethods
62
66
  def new?
63
67
  @attributes[:id].blank?
64
68
  end
69
+ alias :new_record? :new?
65
70
 
66
71
  # List the required attributes for save
67
72
  def required_attributes
68
73
  self.class._module.required_fields
69
74
  end
75
+ alias :required_fields :required_attributes
76
+
77
+ def to_model
78
+ self
79
+ end
70
80
 
71
81
  protected
72
82
 
@@ -99,6 +109,9 @@ module SugarCRM; module AttributeMethods
99
109
  def #{k}=(value)
100
110
  write_attribute :#{k},value
101
111
  end
112
+ def #{k}\?
113
+ has_attribute\? :#{k}
114
+ end
102
115
  ?
103
116
  end
104
117
  self.class.attribute_methods_generated = true
@@ -158,5 +171,9 @@ module SugarCRM; module AttributeMethods
158
171
  @attributes[key] = value
159
172
  end
160
173
 
174
+ def has_attribute?(key)
175
+ @attributes.has_key? key
176
+ end
177
+
161
178
  end; end
162
179
 
data/lib/sugarcrm/base.rb CHANGED
@@ -154,7 +154,7 @@ module SugarCRM; class Base
154
154
  params = {}
155
155
  params[:id] = serialize_id
156
156
  params[:deleted]= {:name => "deleted", :value => "1"}
157
- (self.class.connection.set_entry(self.class._module.name, params).class == Hash)
157
+ @attributes[:deleted] = (self.class.connection.set_entry(self.class._module.name, params).class == Hash)
158
158
  end
159
159
 
160
160
  # Reloads the record from SugarCRM
@@ -4,7 +4,7 @@ module SugarCRM; class Connection
4
4
  login! unless logged_in?
5
5
  json = <<-EOF
6
6
  {
7
- "session": "#{@session.id}"
7
+ "session": "#{@sugar_session_id}"
8
8
  }
9
9
  EOF
10
10
 
@@ -4,8 +4,8 @@ module SugarCRM; class Connection
4
4
  login! unless logged_in?
5
5
  json = <<-EOF
6
6
  {
7
- "session": "#{@session.id}",
8
- "id": #{id}
7
+ "session": "#{@sugar_session_id}",
8
+ "id": "#{id}"
9
9
  }
10
10
  EOF
11
11
  json.gsub!(/^\s{6}/,'')
@@ -10,7 +10,7 @@ module SugarCRM; class Connection
10
10
 
11
11
  json = <<-EOF
12
12
  {
13
- "session": "#{@session.id}",
13
+ "session": "#{@sugar_session_id}",
14
14
  "module_name": "#{module_name}",
15
15
  "ids": #{ids.to_json},
16
16
  "select_fields": #{resolve_fields(module_name, options[:fields])},
@@ -8,7 +8,7 @@ module SugarCRM; class Connection
8
8
 
9
9
  json = <<-EOF
10
10
  {
11
- "session": "#{@session.id}",
11
+ "session": "#{@sugar_session_id}",
12
12
  "module_name": "#{module_name}",
13
13
  "query": "#{query}",
14
14
  "deleted": #{options[:deleted]}
@@ -9,7 +9,7 @@ module SugarCRM; class Connection
9
9
 
10
10
  json = <<-EOF
11
11
  {
12
- "session": "#{@session.id}",
12
+ "session": "#{@sugar_session_id}",
13
13
  "module_name": "#{module_name}",
14
14
  "id": "#{id}",
15
15
  "select_fields": #{resolve_fields(module_name, options[:fields])},
@@ -14,7 +14,7 @@ module SugarCRM; class Connection
14
14
 
15
15
  json = <<-EOF
16
16
  {
17
- "session": "#{@session.id}",
17
+ "session": "#{@sugar_session_id}",
18
18
  "module_name": "#{module_name}",
19
19
  "query": "#{query}",
20
20
  "order_by": "#{options[:order_by]}",
@@ -4,7 +4,7 @@ module SugarCRM; class Connection
4
4
  login! unless logged_in?
5
5
  json = <<-EOF
6
6
  {
7
- "session": "#{@session.id}",
7
+ "session": "#{@sugar_session_id}",
8
8
  "module_name": "#{module_name}"
9
9
  }
10
10
  EOF
@@ -4,7 +4,7 @@ module SugarCRM; class Connection
4
4
  login! unless logged_in?
5
5
  json = <<-EOF
6
6
  {
7
- "session": "#{@session.id}",
7
+ "session": "#{@sugar_session_id}",
8
8
  "id": "#{id}"
9
9
  }
10
10
  EOF
@@ -8,12 +8,12 @@ module SugarCRM; class Connection
8
8
  :query => '',
9
9
  :fields => [],
10
10
  :link_fields => [],
11
- :deleted => ''
11
+ :deleted => 0
12
12
  }.merge! opts
13
13
 
14
14
  json = <<-EOF
15
15
  {
16
- "session": "#{@session.id}",
16
+ "session": "#{@sugar_session_id}",
17
17
  "module_name": "#{module_name}",
18
18
  "module_id": "#{id}",
19
19
  "link_field_name": "#{related_to.downcase}",
@@ -6,7 +6,7 @@ module SugarCRM; class Connection
6
6
 
7
7
  json = <<-EOF
8
8
  {
9
- "session": "#{@session.id}",
9
+ "session": "#{@sugar_session_id}",
10
10
  "ids": #{ids.to_json},
11
11
  "select_fields": "#{options[:select_fields].to_json}"
12
12
  }
@@ -4,7 +4,7 @@ module SugarCRM; class Connection
4
4
  login! unless logged_in?
5
5
  json = <<-EOF
6
6
  {
7
- "session": "#{@session.id}"
7
+ "session": "#{@sugar_session_id}"
8
8
  }
9
9
  EOF
10
10
  json.gsub!(/^\s{6}/,'')
@@ -5,7 +5,7 @@ module SugarCRM; class Connection
5
5
  login! unless logged_in?
6
6
  json = <<-EOF
7
7
  {
8
- "session": "#{@session.id}"
8
+ "session": "#{@sugar_session_id}"
9
9
  }
10
10
  EOF
11
11
  json.gsub!(/^\s{6}/,'')
@@ -5,7 +5,7 @@ module SugarCRM; class Connection
5
5
  json = <<-EOF
6
6
  {
7
7
  "user_auth": {
8
- "session": "#{@session.id}"
8
+ "session": "#{@sugar_session_id}"
9
9
  }
10
10
  }
11
11
  EOF
@@ -4,7 +4,7 @@ module SugarCRM; class Connection
4
4
  login! unless logged_in?
5
5
  json = <<-EOF
6
6
  {
7
- "session": "#{@session.id}"
7
+ "session": "#{@sugar_session_id}"
8
8
  }
9
9
  EOF
10
10
  json.gsub!(/^\s{6}/,'')
@@ -12,7 +12,7 @@ module SugarCRM; class Connection
12
12
 
13
13
  json = <<-EOF
14
14
  {
15
- "session": "#{@session.id}",
15
+ "session": "#{@sugar_session_id}",
16
16
  "search_string": "#{search_string}",
17
17
  "modules": "#{modules}",
18
18
  "offset": #{options[:offset]},
@@ -4,7 +4,7 @@ module SugarCRM; class Connection
4
4
  login! unless logged_in?
5
5
  json = <<-EOF
6
6
  {
7
- "session": "#{@session.id}",
7
+ "session": "#{@sugar_session_id}",
8
8
  "targets": #{targets.to_json},
9
9
  "campaign-id": "#{campaign_id}"
10
10
  }
@@ -1,12 +1,26 @@
1
1
  module SugarCRM; class Connection
2
2
  # Sets a new revision for a document.
3
- def set_document_revision(id, revision)
3
+ def set_document_revision(document_id, revision_number, opts={})
4
+ options = {
5
+ :file => '',
6
+ :file_name => ''
7
+ }.merge! opts
8
+
9
+ # Raise an exception of we try to pass :file, but not :file_name
10
+ if (!options[:file].empty? && options[:file_name].empty?)
11
+ raise ArgumentException, ":file_name must be specified if :file is specified"
12
+ end
13
+
4
14
  login! unless logged_in?
5
15
  json = <<-EOF
6
16
  {
7
- "session": "#{@session.id}",
8
- "document_revision": "#{revision}",
9
- "id": #{id}
17
+ "session": "#{@sugar_session_id}",
18
+ "document_revision": {
19
+ "id": "#{document_id}",
20
+ "filename": "#{options[:file_name]}",
21
+ "file": "#{Base64.encode64(options[:file])}",
22
+ "revision": "#{revision_number}"
23
+ }
10
24
  }
11
25
  EOF
12
26
  json.gsub!(/^\s{6}/,'')
@@ -4,7 +4,7 @@ module SugarCRM; class Connection
4
4
  login! unless logged_in?
5
5
  json = <<-EOF
6
6
  {
7
- "session": "#{@session.id}",
7
+ "session": "#{@sugar_session_id}",
8
8
  "module_name": "#{module_name}",
9
9
  "name_value_list": #{name_value_lists.to_json}
10
10
  }
@@ -4,7 +4,7 @@ module SugarCRM; class Connection
4
4
  login! unless logged_in?
5
5
  json = <<-EOF
6
6
  {
7
- "session": "#{@session.id}",
7
+ "session": "#{@sugar_session_id}",
8
8
  "module_name": "#{module_name}",
9
9
  "name_value_list": #{name_value_list.to_json}
10
10
  }
@@ -9,7 +9,7 @@ module SugarCRM; class Connection
9
9
  login! unless logged_in?
10
10
  json = <<-EOF
11
11
  {
12
- "session": "#{@session.id}",
12
+ "session": "#{@sugar_session_id}",
13
13
  "note": {
14
14
  "id": "#{id}",
15
15
  "filename": "#{filename}",
@@ -9,7 +9,7 @@ module SugarCRM; class Connection
9
9
  raise ArgumentError, "related_ids must be an Array" unless related_ids.class == Array
10
10
  json = <<-EOF
11
11
  {
12
- "session": "#{@session.id}",
12
+ "session": "#{@sugar_session_id}",
13
13
  "module_name": "#{module_name}",
14
14
  "module_id": "#{module_id}",
15
15
  "link_field_name": "#{link_field_name}",
@@ -9,7 +9,7 @@ module SugarCRM; class Connection
9
9
 
10
10
  json = <<-EOF
11
11
  {
12
- "session": "#{@session.id}",
12
+ "session": "#{@sugar_session_id}",
13
13
  "module_names": "#{module_names.to_json}",
14
14
  "module_ids": #{module_ids.to_json},
15
15
  "link_field_names": #{link_field_names.to_json},
@@ -8,8 +8,8 @@ module SugarCRM; class Connection
8
8
  attr :url, true
9
9
  attr :user, false
10
10
  attr :pass, false
11
- attr :session_id, true
12
- attr_accessor :session
11
+ attr :session, true
12
+ attr :sugar_session_id, true
13
13
  attr :connection, true
14
14
  attr :options, true
15
15
  attr :request, true
@@ -29,21 +29,25 @@ module SugarCRM; class Connection
29
29
  @response = ""
30
30
  resolve_url
31
31
  login!
32
- @session.update_config({:base_url => url, :username => user, :password => pass}) if @session
33
32
  self
34
33
  end
35
34
 
36
35
  # Check to see if we are logged in
37
36
  def logged_in?
38
37
  connect! unless connected?
39
- @session_id ? true : false
38
+ @sugar_session_id ? true : false
40
39
  end
41
40
 
42
41
  # Login
43
42
  def login!
44
- @session_id = login["id"]
43
+ @sugar_session_id = login["id"]
45
44
  raise SugarCRM::LoginError, "Invalid Login" unless logged_in?
46
45
  end
46
+
47
+ def logout
48
+ logout
49
+ @sugar_session_id = nil
50
+ end
47
51
 
48
52
  # Check to see if we are connected
49
53
  def connected?