cheddargetter_client_rails 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+ gem "activesupport", ">= 2.3.5"
3
+ gem "cheddargetter_client_ruby", ">= 0"
4
+
5
+ group :development do
6
+ gem "rspec", "~> 2.3.0"
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.6.0"
9
+ gem "rcov", ">= 0"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.7)
5
+ cheddargetter_client_ruby (0.3.1)
6
+ httparty (>= 0.4.3)
7
+ httparty (>= 0.4.3)
8
+ crack (0.1.8)
9
+ diff-lcs (1.1.2)
10
+ git (1.2.5)
11
+ httparty (0.7.7)
12
+ crack (= 0.1.8)
13
+ jeweler (1.6.0)
14
+ bundler (~> 1.0.0)
15
+ git (>= 1.2.5)
16
+ rake
17
+ rake (0.8.7)
18
+ rcov (0.9.9)
19
+ rspec (2.3.0)
20
+ rspec-core (~> 2.3.0)
21
+ rspec-expectations (~> 2.3.0)
22
+ rspec-mocks (~> 2.3.0)
23
+ rspec-core (2.3.1)
24
+ rspec-expectations (2.3.0)
25
+ diff-lcs (~> 1.1.2)
26
+ rspec-mocks (2.3.0)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ activesupport (>= 2.3.5)
33
+ bundler (~> 1.0.0)
34
+ cheddargetter_client_ruby
35
+ jeweler (~> 1.6.0)
36
+ rcov
37
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Brent Wooden
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,142 @@
1
+ = cheddargetter_client_rails
2
+
3
+ The CheddarGetter rails client aims to tie the CheddarGetter API closely into ActiveRecord models.
4
+ Much of the heavy lifting with the api itself is done through the cheddargetter_client_ruby gem.
5
+
6
+ == Usage
7
+
8
+ === In the model
9
+
10
+ class User < ActiveRecord::Base
11
+ cheddargetter_billable_on :customer_code, :shared_columns => {
12
+ :email => :email,
13
+ :firstName => :first_name,
14
+ :lastName => :last_name,
15
+ :planCode => :plan_code
16
+ }
17
+ end
18
+
19
+ The first argument is the unique identifier column that CheddarGetter uses for the customers' customerCode.
20
+ It can be any column as long as it is unique.
21
+
22
+ The shared columns hash are other attributes that appear both in your records for the user and CheddarGetter's records. When the save is called on the subscription object it grabs these attributes from your ActiveRecord
23
+ record. This is to prevent duplicate form inputs asking the user more than once for the same information.
24
+
25
+ The planCode shared attribute may also be set as string, as sometimes you want everyone on the same plan.
26
+
27
+ class User < ActiveRecord::Base
28
+ cheddargetter_billable_on :customer_code, :shared_columns => {
29
+ :email => :email,
30
+ :firstName => :first_name,
31
+ :lastName => :last_name,
32
+ :planCode => 'PAID_PLAN'
33
+ }
34
+ end
35
+
36
+ === In the controller
37
+
38
+ Make sure the subscription is always set on the user as some data may only exist there.
39
+
40
+ class CreditCardsController < ApplicationController
41
+ def edit
42
+ @user = current_user
43
+ end
44
+
45
+ def update
46
+ @user = current_user
47
+
48
+ if @user.save_subscription(params[:cheddargetter_client_rails_subscription])
49
+ redirect_to edit_credit_card_path, :flash => {:success => 'Billing information updated'}
50
+ else
51
+ render 'edit'
52
+ end
53
+ end
54
+ end
55
+
56
+ Or in a user controller
57
+
58
+ class UsersController < ApplicationController
59
+ def new
60
+ @user = User.new
61
+ end
62
+
63
+ def create
64
+ @user = User.new
65
+ @user.build_subscription(params[:cheddargetter_client_rails_subscription])
66
+
67
+ if @user.save
68
+ redirect_to after_creation_path, :flash => {:success => 'User successfully created'}
69
+ else
70
+ render 'new'
71
+ end
72
+ end
73
+ end
74
+
75
+ The user save will take care of saving the subscription to CheddarGetter.
76
+
77
+ === In the view
78
+
79
+ Do something like this, if it is a user form use fields_for @user.subscription inside the user form.
80
+
81
+ <%= form_for(@user.subscription, :url => credit_cards_path) do |subscription| %>
82
+ <fieldset>
83
+ <%= subscription.hidden_field :planCode, :value => 'FREE_PLAN', :class => 'jq_planCodeValue' %>
84
+
85
+ <dl>
86
+ <dt>First Name:</dt>
87
+ <dd>
88
+ <%= subscription.text_field :ccFirstName,:autocomplete => "off" %>
89
+ </dd>
90
+
91
+ <dt>Last Name:</dt>
92
+ <dd>
93
+ <%= subscription.text_field :ccLastName, :autocomplete => "off" %>
94
+ </dd>
95
+
96
+ <dt>Card Number:</dt>
97
+ <dd>
98
+ <%= subscription.text_field :ccNumber,:autocomplete => "off" %>
99
+ </dd>
100
+ <dt>Expiration Date:</dt>
101
+ <dd>
102
+ <%= subscription.text_field :ccExpiration, :autocomplete => 'off' %>
103
+ </dd>
104
+ <dt>Address:</dt>
105
+ <dd>
106
+ <%= subscription.text_field :ccAddress, :autocomplete => 'off' %>
107
+ </dd>
108
+ <dt>City:</dt>
109
+ <dd>
110
+ <%= subscription.text_field :ccCity, :autocomplete => 'off' %>
111
+ </dd>
112
+ <dt>State:</dt>
113
+ <dd>
114
+ <%= subscription.text_field :ccState, :autocomplete => 'off' %>
115
+ </dd>
116
+ <dt>Zip Code:</dt>
117
+ <dd>
118
+ <%= subscription.text_field :zip, :autocomplete => 'off' %>
119
+ </dd>
120
+ <dt>Country:</dt>
121
+ <dd>
122
+ <%= subscription.text_field :ccCountry, :autocomplete => 'off' %>
123
+ </dd>
124
+ </fieldset>
125
+ <% end %>
126
+
127
+
128
+ == Contributing to cheddargetter_client_rails
129
+
130
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
131
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
132
+ * Fork the project
133
+ * Start a feature/bugfix branch
134
+ * Commit and push until you are happy with your contribution
135
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
136
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
137
+
138
+ == Copyright
139
+
140
+ Copyright (c) 2011 Brent Wooden. See LICENSE.txt for
141
+ further details.
142
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "cheddargetter_client_rails"
18
+ gem.homepage = "http://github.com/BrentW/cheddargetter_client_rails"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Integrates CheddarGetter api with Active Record}
21
+ gem.description = %Q{Integrates CheddarGetter api with Active Record. Uses cheddargetter_client_ruby.}
22
+ gem.email = "brent.wooden@gmail.com"
23
+ gem.authors = ["Brent Wooden"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "cheddargetter_client_rails #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.8
@@ -0,0 +1,73 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{cheddargetter_client_rails}
8
+ s.version = "0.1.8"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brent Wooden"]
12
+ s.date = %q{2011-05-11}
13
+ s.description = %q{Integrates CheddarGetter api with Active Record. Uses cheddargetter_client_ruby.}
14
+ s.email = %q{brent.wooden@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "cheddargetter_client_rails.gemspec",
29
+ "features/cheddargetter_client_rails.feature",
30
+ "features/step_definitions/cheddargetter_client_rails_steps.rb",
31
+ "features/support/env.rb",
32
+ "lib/cheddargetter_client_rails.rb",
33
+ "lib/cheddargetter_client_rails/subscription.rb",
34
+ "spec/cheddargetter_client_rails/subscription_spec.rb",
35
+ "spec/cheddargetter_client_rails_spec.rb",
36
+ "spec/fixtures/users.yml",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/BrentW/cheddargetter_client_rails}
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.7}
43
+ s.summary = %q{Integrates CheddarGetter api with Active Record}
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.5"])
51
+ s.add_runtime_dependency(%q<cheddargetter_client_ruby>, [">= 0"])
52
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
53
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.0"])
55
+ s.add_development_dependency(%q<rcov>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<activesupport>, [">= 2.3.5"])
58
+ s.add_dependency(%q<cheddargetter_client_ruby>, [">= 0"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<activesupport>, [">= 2.3.5"])
66
+ s.add_dependency(%q<cheddargetter_client_ruby>, [">= 0"])
67
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
68
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
69
+ s.add_dependency(%q<jeweler>, ["~> 1.6.0"])
70
+ s.add_dependency(%q<rcov>, [">= 0"])
71
+ end
72
+ end
73
+
@@ -0,0 +1,9 @@
1
+ Feature: something something
2
+ In order to something something
3
+ A user something something
4
+ something something something
5
+
6
+ Scenario: something something
7
+ Given inspiration
8
+ When I create a sweet new gem
9
+ Then everyone should see how awesome I am
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ begin
3
+ Bundler.setup(:default, :development)
4
+ rescue Bundler::BundlerError => e
5
+ $stderr.puts e.message
6
+ $stderr.puts "Run `bundle install` to install missing gems"
7
+ exit e.status_code
8
+ end
9
+
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
11
+ require 'cheddargetter_client_rails'
12
+
13
+ require 'rspec/expectations'
@@ -0,0 +1,120 @@
1
+ require 'active_support'
2
+ require 'cheddargetter_client_ruby'
3
+
4
+ module CheddargetterClientRails
5
+ autoload :Subscription, 'cheddargetter_client_rails/subscription'
6
+
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+
10
+ def subscription
11
+ @subscription ||= Subscription.new
12
+ end
13
+
14
+ def subscription=(value)
15
+ @subscription = value
16
+ end
17
+
18
+ def validate_subscription
19
+ supplement_subscription_fields
20
+
21
+ if !skip_cheddargetter && new_record? && !subscription.valid?
22
+ errors.add(:subscription, 'problem')
23
+ end
24
+ end
25
+
26
+ def supplement_subscription_fields
27
+ self.class.shared_columns.each do |subscription_column, user_attribute|
28
+ if(subscription_column == :planCode && user_attribute.is_a?(String)) #user can specify planCode as a string
29
+ subscription.send(subscription_column.to_s + '=', user_attribute)
30
+ else
31
+ subscription.send(subscription_column.to_s + '=', send(user_attribute))
32
+ end
33
+ end
34
+ end
35
+
36
+ def create_subscription
37
+ raise ArgumentError, 'Customer code is not set on record.' if !customer_code_column_value && !subscription.customerCode
38
+ subscription.customerCode = customer_code_column_value if !subscription.customerCode
39
+ subscription.create unless skip_cheddargetter
40
+ end
41
+
42
+ def current_subscription
43
+ @current_subscription ||= CheddargetterClientRails::Subscription.get(customer_code_column_value) if customer_code_column_value
44
+ end
45
+
46
+ def destroy_subscription
47
+ current_subscription.try(:destroy)
48
+ end
49
+
50
+ def customer_code_column_value
51
+ send(self.class.send(:customer_code_column)) if self.class.send(:customer_code_column)
52
+ end
53
+
54
+ def build_subscription(attributes_hash)
55
+ # set attributes from current cheddargetter subscription, then
56
+ # replaces any values with supplied data
57
+ new_subscription = CheddargetterClientRails::Subscription.new
58
+ if old_subscription = current_subscription
59
+ old_subscription.instance_variables_hash.each do |key, value|
60
+ new_subscription.send(key.to_s + '=', value)
61
+ end
62
+ end
63
+
64
+ attributes_hash.each do |key, value|
65
+ new_subscription.send(key.to_s + '=', value)
66
+ end
67
+
68
+ self.subscription = new_subscription
69
+ new_subscription
70
+ end
71
+
72
+ def save_subscription(attributes_hash)
73
+ build_subscription(attributes_hash)
74
+ subscription.save
75
+ end
76
+ end
77
+
78
+ module ClassMethods
79
+ def cheddargetter_billable_on(*args)
80
+ raise ArgumentError.new('Must supply customer code column.') if args.length < 1
81
+ self.customer_code_column = args.shift
82
+ raise ArgumentError.new("Record does not respond to #{customer_code_column.to_s}.") if !responds_to_customer_code_column?
83
+
84
+ if args.length > 0
85
+ self.shared_columns = args.shift[:shared_columns]
86
+ end
87
+
88
+ attr_accessor :skip_cheddargetter
89
+
90
+ validate :validate_subscription
91
+ after_create :create_subscription
92
+ before_destroy :destroy_subscription
93
+ end
94
+
95
+ def responds_to_customer_code_column?
96
+ self.instance_methods.include?(customer_code_column.to_s) ||
97
+ self.column_names.include?(customer_code_column.to_s)
98
+ end
99
+
100
+ def customer_code_column
101
+ @customer_code_column
102
+ end
103
+
104
+ def customer_code_column=(column)
105
+ @customer_code_column = column
106
+ end
107
+
108
+ def shared_columns
109
+ @shared_columns
110
+ end
111
+
112
+ def shared_columns=(columns)
113
+ @shared_columns = columns
114
+ end
115
+ end
116
+ end
117
+
118
+ class ActiveRecord::Base
119
+ include CheddargetterClientRails
120
+ end