The CheddarGetter rails client aims to tie the CheddarGetter API closely into ActiveRecord models.
Much of the heavy lifting with the api itself is done through the cheddargetter_client_ruby gem.
Run the generator to get the config files
rails g cheddargetter
This will create /config/initializers/cheddargetter_client.rb and /config/cheddargetter.yml Please edit /config/cheddargetter.yml to include your CheddarGetter credentials.
It can be very simple if you go with the default columns, ie:
:customerCode => :id :email => :email, :firstName => :first_name, :lastName => :last_name, :planCode => :plan_code
Then the declaration in the model is simply:
class User < ActiveRecord::Base has_subscription
end
These are the only required columns however you can change their names locally very easily.
class User < ActiveRecord::Base has_subscription :customerCode => :customer_code,
:firstName => :name :lastName => :l_name :email => :business_email :planCode => “FREE_PLAN”
end
Note that the plan code can also take a string.
The has_subscription will also takes additional key/values of items that appear both in your records for the user and CheddarGetter’s records. For instance zip code is a common one. Here are others:
:ccFirstName :ccLastName :ccExpiration :ccNumber :ccCountry :ccAddress :ccCity :ccState :company :zip
When the save is called on the subscription object or create is called on the user it grabs all shared attributes from your ActiveRecord record.
Make sure the subscription is always set on the user as some data may only exist there.
class CreditCardsController < ApplicationController def edit @user = current_user end
def update @user = current_user
if @user.save_subscription(params) redirect_to edit_credit_card_path, :flash => {:success => ‘Billing information updated’} else
render ‘edit’ end end end
Or in a user controller
class UsersController < ApplicationController
def new @user = User.new end
def create @user = User.new @user.build_subscription(params)
if @user.save redirect_to after_creation_path, :flash => {:success => ‘User successfully created’} else
render ‘new’ end end end
The user save will take care of saving the subscription to CheddarGetter.
Do something like this, if it is a user form use fields_for @user.subscription inside the user form.
<%= form_for(@user.subscription, :url => credit_cards_path) do |subscription| %> <fieldset> <%= subscription.hidden_field :planCode, :value => ‘FREE_PLAN’, :class => ‘jq_planCodeValue’ %> <dl> <dt>First Name:</dt> <dd> <%= subscription.text_field :ccFirstName,:autocomplete => “off” %> </dd>
<dt>Last Name:</dt> <dd> <%= subscription.text_field :ccLastName, :autocomplete => “off” %> </dd> <dt>Card Number:</dt> <dd> <%= subscription.text_field :ccNumber,:autocomplete => “off” %> </dd> <dt>Expiration Date:</dt> <dd> <%= subscription.text_field :ccExpiration, :autocomplete => ‘off’ %> </dd> <dt>Address:</dt> <dd> <%= subscription.text_field :ccAddress, :autocomplete => ‘off’ %> </dd> <dt>City:</dt> <dd> <%= subscription.text_field :ccCity, :autocomplete => ‘off’ %> </dd> <dt>State:</dt> <dd> <%= subscription.text_field :ccState, :autocomplete => ‘off’ %> </dd> <dt>Zip Code:</dt> <dd> <%= subscription.text_field :zip, :autocomplete => ‘off’ %> </dd> <dt>Country:</dt> <dd> <%= subscription.text_field :ccCountry, :autocomplete => ‘off’ %> </dd> </dt </fieldset> <% end %>
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it
-
Fork the project
-
Start a feature/bugfix branch
-
Commit and push until you are happy with your contribution
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
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.
Copyright © 2011 Brent Wooden. See LICENSE.txt for further details.