Friday 26 December 2014

Example of Single Table Inheritance in Rails 4



Single-table inheritance (STI) is the practice of storing multiple types of values in the same table, where each record includes a field indicating its :type , and the table includes a column for every field of all the types it stores. In Rails, the type column is used to determine which type of model to instantiate for each row; a row with type = 'Article' will make Rails call Article.new when turning that row into an object.

create_table 'employees' do |t|
  t.string  'type'
  t.string  'name'
  t.text    'email'
end

When we went with this design in the first place. After all, articles, videos, discussions and quizzes don’t seem like subtypes of one another. All these types of content have to be put into a sequence to make a course. So, each content item belongs to a course, and we use acts_as_list to keep the items, or ‘steps’, in order. This is most easily realised if all the steps live in the same table.

class Course < ActiveRecord::Base
  has_many :steps
end

class Step < ActiveRecord::Base
  belongs_to :course
  acts_as_list scope: :course
end

Schema of this model

create_table 'courses' do |t|
  t.string 'title'
end

create_table 'steps' do |t|
  t.string  'type'
  t.integer 'course_id'
  t.integer 'position',   null: false
  t.string  'title'
  t.text    'body'
  t.integer 'asset_id'
  t.string  'url'
  t.string  'copyright'
end

Then we have the content types themselves, for example Article hasbody and copyright attributes, Video has asset_id and copyright, andExercise has body and url.
class Article < Step
  validates :body,      presence: true
  validates :copyright, length: { maximum: 255 }
end

class Video < Step
  validates :asset_id,  presence: true
  validates :copyright, length: { maximum: 255 }
end

class Exercise < Step
  validates :body, :url, presence: true
end


STI should be used if your submodels will share the same attributes but need different behavior. If you plan to add 10 columns only used by one submodel, using different tables might be a better solution.

Friday 19 December 2014

About Helper Method in Rails 4

The method helper_method is to explicitly share some methods defined in the controller to make them available for the view. This is used for any method that you need to access from both controllers and views . Like suppose we use application_controller.rb for example we add a method by name 
current_user like this

#application_controller.rb
helper_method :current_account
private
def current_account
#logged_user is method which return logged user find_by it's :session_id
  @current_account ||= logged_user.account if logged_user
  @current_account
end 

Now here helper_method create a method bye name of current_account and this is share for all controller which are inherited by #application_controller.rb. 

NOTE : helper_method not accessible in model.rb . Helper method use in controller only for view and if we write a method in xxxx_helper.rb then this method is only accessible in xxxx_controller.rb like  if we write code in account_helper.rb   it's only accessible in account_controller.rb.


Stripe for PAYMENT in Ruby on Rails application



Stripe is a company that provides a way for individuals and businesses to accept payments over the Internet.


Easy to implement a payment gateway by Stripe in Ruby on Rails Application
It's Provide many function for Client like

1.Create , Update ,Delete --> charges 
2.Create , Update ,Delete --> subscription
3.Create , Update ,Delete --> plain
4.Create , Update ,Delete --> coupons , customer,refund, discounts 


Step 1: adding Gem gem in Gemfile

gem 'stripe',  :git => 'https://github.com/stripe/stripe-ruby'

Step 2: Run 'bundle'
Step 3 : rails generate controller subscriptions

app/controller/subscriptions_controller.rb

def create 
  customer = Stripe::Customer.create(
      :card => params[:stripeToken],
      :plan => 'planname',
      :email => params[:stripeEmail],
  )

rescue Stripe::CardError => e flash[:error] = e.message
 redirect_to charges_path
end
step :4
add in config/route.rb
resources :subscriptions
step: 5 

config/initializers/stripe.rb:

Rails.configuration.stripe = {
 :publishable_key => 'PUBLISHABLE_KEY',
 :secret_key => 'SECRET_KEY'
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]




Step :6



Now let’s create new.html.erb under app/views/subscriptions, which is going to be our checkout page, which will display a credit card overlay which includes validation and error handling.
 


<%= form_tag subscriptions_path, :class => 'subscription_form' do %> 



<script src="https://checkout.stripe.com/checkout.js"


class="stripe-button"


data-key="<%= Rails.configuration.stripe[:publishable_key] %>"


data-description="Description about palin" 


data-amount="Plain amount" data-label='Pay' >


</script>



step :7


Finally let’s make a create.html.erb view under app/views/subscriptions that shows users a success message.


<h2>Thanks, you paid <strong>$5.00</strong>!</h2>








Friday 5 December 2014

Difference Between Gem and Rails Engine

An Engine in rails terminology is a actually a sub-application of a web-application. For instance, something like a blog, a forum, or simple authentication: these are not full-blown applications, but pages/views/controllers/models that can be added to any rails application.
 gem is a ruby library, which can be found on http://rubygems.org and it is the standard (only) way to package and distribute ruby code to other ruby programmer. 
So we say:
  • A gem: is a generic library, which can be easily installed, which are version-managed, have dependencies and such.
  • An engine: is a sub-application of a Rails application, and since Rails 3 these are distributed as a gem.
So when will you use one or the other:
  • create a gem if you want to share ruby-functionality
  • create an engine (and package it in a gem) if you have parts of your rails application that can be used more generally.

Friday 28 November 2014

Error invalid Useraccount / Password in Rails 4


config/enviroments/development.rb

config.action_mailer.raise_delivery_errors = trueconfig.action_mailer.delivery_method = :smtphost = 'localhost:3000'config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :user_name            => "emailid",
    :password             => 'password',
    :authentication       => "plain",
    :enable_starttls_auto => true}

Salesforce CRM vs. Zoho: A Comparative Analysis

Introduction: Selecting the right customer relationship management (CRM) software is crucial for businesses seeking to streamline their sal...