Skip to content

Instantly share code, notes, and snippets.

@tamouse
Created February 21, 2014 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamouse/9126168 to your computer and use it in GitHub Desktop.
Save tamouse/9126168 to your computer and use it in GitHub Desktop.
Problem with reform from_hash method missing for a nested model.
class ExtraUserInformation < ActiveRecord::Base
belongs_to :user
has_one :credit_card
has_one :billing_address, :class_name => "Address"
has_one :shipping_address, :class_name => "Address"
attr_accessible :billing_address, :credit_card, :company, :customer_code, :email, :fax, :first_name, :last_name, :phone, :shipping_address, :website
end
class Address < ActiveRecord::Base
belongs_to :extra_user_information
attr_accessible :address_code, :country_code, :locality, :postal_code, :region, :street1, :street2
end
class CreditCard < ActiveRecord::Base
belongs_to :extra_user_information
attr_accessible :code
end
class Subscription < ActiveRecord::Base
belongs_to :user
attr_accessible :end_date, :start_date, :transaction_code, :customer_code, :premium_plan, :user
validates :start_date, :transaction_code, :customer_code, :premium_plan, :user, :presence => true
def self.latest
order('start_date DESC').first
end
def active?
end_date.nil? || end_date > Date.today
end
def deactivate
self.end_date = Date.today
save!
end
end
class PremiumReward < ActiveRecord::Base
belongs_to :user
attr_accessible :customer_code # code for this user from payment gateway
attr_accessible :transaction_code # code for this transaction from payment gateway
attr_accessible :gift_info # yaml-ized string of gift info (i.e., price, variant, count, etc)
attr_accessible :purchase_date # date of the transaction
attr_accessible :sent_date # date the item was sent to the user
end
class SubscriptionForm < Reform::Form
property :extra_user_information do
property :first_name, :on => :extra_user_information
property :last_name, :on => :extra_user_information
property :email, :on => :extra_user_information
property :phone, :on => :extra_user_information
property :fax, :on => :extra_user_information
property :website, :on => :extra_user_information
property :company, :on => :extra_user_information
property :customer_code, :on => :extra_user_information
property :credit_card do
property :code, :on => :credit_card
property :number, :empty => true
property :exp_month, :empty => true
property :exp_year, :empty => true
property :cvv, :empty => true
end
property :shipping_address do
property :street1, :on => :shipping_address
property :street2, :on => :shipping_address
property :locality, :on => :shipping_address
property :region, :on => :shipping_address
property :postal_code, :on => :shipping_address
property :country_code, :on => :shipping_address
property :use_billing_address, :empty => true
end
property :billing_address do
property :street1, :on => :billing_address
property :street2, :on => :billing_address
property :locality, :on => :billing_address
property :region, :on => :billing_address
property :postal_code, :on => :billing_address
property :country_code, :on => :billing_address
end
end
property :premium_subscription do
property :start_date, :on => :premium_subscription
property :transaction_code, :on => :premium_subscription
property :customer_code, :on => :premium_subscription
property :chosen_plan, :empty => true
end
property :premium_reward do
property :customer_code, :on => :premium_reward
property :transaction_code, :on => :premium_reward
property :purchase_date, :on => :premium_reward
property :gift_info, :on => :premium_reward
property :shirt_size, :empty => true
property :shirt_color, :empty => true
property :quantity, :empty => true #:on => :premium_reward
property :accept_premium_reward, :empty => true
end
property :opt_in_to_bt_marketing, :empty => true
property :opt_in_to_partner_marketing, :empty => true
property :agree_to_tos, :empty => true
model :user, :on => :user
def initialize(user)
@user = user
super
end
def submit(params)
self.save # this is where it blows up
:complete
end
end
class SubscriptionsController < ApplicationController
authorize_resource
def new
redirect_to current_user, :notice => I18n.t('subscription.already_pro') if current_user.is_paid?
@user = build_subscription(current_user)
@subscription_form = SubscriptionForm.new(@user)
end
def create
@user = build_subscription(current_user)
@subscription_form = SubscriptionForm.new(@user)
case @subscription_form.submit(params[:user])
when :complete
redirect_to @user, :notice => t('subscription.congratulations')
when :needs_confirmation
render 'confirm'
else
render 'new'
end
end
private
def build_subscription(user)
# For Reform::Form, need to build out items
user.build_extra_user_information
user.extra_user_information.build_credit_card
user.extra_user_information.build_billing_address
user.extra_user_information.build_shipping_address
user.build_premium_subscription
user.build_premium_reward
user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment