= cheddargetter_client_rails 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. == Usage === In the model 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. === In the controller 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[:cheddargetter_client_rails_subscription]) 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[:cheddargetter_client_rails_subscription]) 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. === In the view 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| %>
<%= subscription.hidden_field :planCode, :value => 'FREE_PLAN', :class => 'jq_planCodeValue' %>
First Name:
<%= subscription.text_field :ccFirstName,:autocomplete => "off" %>
Last Name:
<%= subscription.text_field :ccLastName, :autocomplete => "off" %>
Card Number:
<%= subscription.text_field :ccNumber,:autocomplete => "off" %>
Expiration Date:
<%= subscription.text_field :ccExpiration, :autocomplete => 'off' %>
Address:
<%= subscription.text_field :ccAddress, :autocomplete => 'off' %>
City:
<%= subscription.text_field :ccCity, :autocomplete => 'off' %>
State:
<%= subscription.text_field :ccState, :autocomplete => 'off' %>
Zip Code:
<%= subscription.text_field :zip, :autocomplete => 'off' %>
Country:
<%= subscription.text_field :ccCountry, :autocomplete => 'off' %>
<% end %> == Contributing to cheddargetter_client_rails * 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 Copyright (c) 2011 Brent Wooden. See LICENSE.txt for further details.