method

cache

Importance_3
v2.3.8 - Show latest stable - 5 notes - Class: Rails
cache() public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Show source
Register or log in to add new notes.
August 7, 2009
3 thanks

Documentation

This method only returns a cache manager object of sorts, to see what you can do with it, see ActiveSupport::Cache::Store.

August 7, 2009
2 thanks

Most common use case

Most common use case is probably:

Rails.cache.fetch "some key" do
  compute some value
end

This computes some value and caches it. Subsequent invocations will return cached value (as long as it is still cached).

October 27, 2016 - (<= v3.2.13)
1 thank

An example, with expiration time set

e.g.:

Rails.cache.fetch("out_of_stock_products", :expires_in => 5.minutes) do
  Product.all.joins(:inventory).conditions.where("inventory.quantity = 0")
end
November 28, 2016
0 thanks

Track Changes To Your Model’s Data with Paper Trail Gem

Paper trail lets us track all the changes in the models data for the purpose of its editing and versioning. By using this gem, we can see how a model looks, at every stage of its life-cycle and we can take it back to any version of the model data and we can even undo all the changes after a record has been destroyed so as to restore it completely.

gempaper_trail’ run bundle install to install it

After bundle install you can run the following command for adding versions table to your database

bundle exec rails generate paper_trail:install

bundle exec rake db:migrate

Add has_paper_trail method to your models for tracking.

class Product < ActiveRecord::Base
 	has_paper_trail

end

If you are using a current_user method then you can track who is responsible for particular change by the below callback

class ApplicationController

before_action :set_paper_trail_whodunnit end

Features of paper trail gem

It stores each and every change happened in the model  like create, update and destroy
It does not store any updates unless if any modifications
It allows you to get every version including the actual and even once destroyed

Basic Usage

Now you have a versions method which returns all the changes in particular model

product = Product.find 2

product.versions

# [<PaperTrail::Version>, <PaperTrail::Version>, ...]

Based on the vesrion you can find the changes that happened in a model

v = product.versions.last

   v.event # it returns update / create / destroy
   v.whodunnit #it returns a user id who did this
   v.reify the poduct as it was before the update

you can navigate versions by using previous_version and next_version methods

product = product.previous_version

   product = product.next_version #it returns nil if there is no object

   product.versions.last.whodunnit #it returns the user who did the particular change

For More Read From Here : http://www.railscarma.com/blog/technical-articles/track-changes-models-data-paper-trail-gem/

November 8, 2016
0 thanks

Ways of Handling PayPal Refunds in Rails

PayPal Checkout includes payment solutions ranging from simple to robust that let the merchants as well as developers choose an integration option that can be the best-suited for their website and customers. In order to integrate Paypal Payment Gateway, we need to do the following:

  1. Have a PayPal business or Premier account.

  2. Create a PayPal app and get an access token. When we create a PayPal app, PayPal generates a set of OAuth client_id and secret keys for the application. PayPal generates these keys for both the PayPal sandbox and Live environments. To get an access token, pass the client-id:secret credentials in the Authorization header. We use the access token for authentication when we make REST API requests.

  3. To perform an end-to-end test of our Express Checkout with In-Context integration, create both merchant and buyer accounts in the PayPal sandbox environment.

www.sandbox.paypal.com/in/webapps/mpp/home Merchant : Select the Business account type and enter an email address and password. Buyer : Select the Personal account type and enter a high PayPal balance, such as 5000.

Once we create Paypal sandbox account then click on the “Profile” link for that account, look under the tab “API Credentials”. We will have the following information;

Paypal API Username
Paypal API Signature
Paypal API Password

Note: When we are ready to go live we would just use the credentials from our real paypal account instead of the ones from our sandbox account. The credentials can be found in the “My Profile” area under the left hand tab “My Selling Tools” under the option “API access”

How to handle Paypal refunds in Rails:

Method 1:

Paypal Rest API;

For more complex merchant sites, direct calls to PayPal APIs for an Express Checkout integration may be a more appropriate integration. REST APIs — We can develop an Express Checkout integration using PayPal REST APIs.

To integrate the Express Checkout with In-Context flow; developer.paypal.com/docs/api/ OR PayPal REST API Ruby SDK (paypal-sdk-rest gem): The PayPal REST SDK provides Ruby APIs to create, process and manage payment.

Installation: Add the gem our application, in Gemfile:

gem 'paypal-sdk-rest'

And then execute:

$ bundle install

Configuration:

rails g paypal:sdk:install

Refunds a transaction: Any transaction we can issue a refund (Both direct and captured payments):

Refund a completed direct payment (sale)
Refund an authorized and captured payment (capture)

Refund a completed payment (sale): If we must refund a completed payment, or sale, provide the sale id given to us in response to a completed payment along with an empty JSON payload for a full refund and for partial refunds, we can instead include an amount object in the JSON payload.

curl -v https://api.sandbox.paypal.com/v1/payments /sale/CARMAXYZC6136044L/refund \
-H "Content-Type:application/json" \
-H "Authorization: Bearer Access-Token" \
-d '{}'

Note: We should substitute all call-specific parameters, such as tokens and IDs, with our own. Response state of the refund:

pending- The refund is pending.
completed- The refund has successfully completed.
failed- The refund failed.

Refund a captured payment; We can also refund a captured payment: API:

https://api.paypal.com/v1/payments/capture/{capture_id}/refund

Note: We must provide an amount object for both full and partial refunds.

curl -v https://api.sandbox.paypal.com/v1/payments/capture/CARMAXYZC6136044L/refund \
-H "Content-Type:application/json" \
-H "Authorization: Bearer Access-Token" \
-d '{
"amount":
{
"currency": "USD",
"total": "50.54"
},
"description": "This is the capture refund description."
}'

Reference: https://developer.paypal.com

Method 2:

Paypal refund by using Active Merchant Gem:

ActiveMerchant Integration: http://railscasts.com/episodes/145-integrating-active-merchant

In our Application;

config/environment/development.rb

config.after_initialize do

ActiveMerchant::Billing::Base.mode = :test
::GATEWAY = ActiveMerchant::Billing::PaypalGateway.new(
:login => "seller_1229899173_biz_api1.xyz.com",
:password => "FXWU58S7KXFC6HBE",
:signature =>AGjv6SW.mTiKxtkm6L9DcSUCUgePAUDQ3L- kTdszkPG8mRfjaRZDYtSu"

)

end

Refunds a transaction: Take a look into paypal_common_api.rb file in Active Merchant Gem;

https://github.com/activemerchant/active_merchant/blob/master/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb
  1. For a full refund pass nil for the amount:

gateway.refund nil, 'CARMAXYZC6136044L'

This will automatically make the :refund_type be “Full”.

  1. For a partial refund just pass the amount as usual:

gateway.refund 100, 'CARMAXYZC6136044L'
def refund(money, identification, options = {})
commit 'RefundTransaction', build_refund_request(money,  identification, options)
end

Ex:

Gateway.refund(nil,'CARMAXYZC6136044L') => Full Refund.

Gateway.refund(798,'CARMAXYZC6136044L') => Partial Refund.

Reference: http://www.rubydoc.info/github/Shopify/active_merchant/ActiveMerchant/Billing/PaypalCommonAPI

Method3: Read More From Here==> http://www.railscarma.com/blog/technical-articles/ways-handling-paypal-refunds-rails/