Monday, July 17, 2017

Spree Commerce Customization

Spree Commerce Customization

Spree Commerce Customization

  1. Create an image with 116x50 pixels named “spree_50.png” and put it at app/assets/images/logo/.

  2. Using a new file to overwrite the original one.

    • Create a new file app/models/spree/app_configuration.rb.

    • copy the content of
      (user)/.rvm/gems/ruby02.X.X/gems/spree_core/app/model/spree/app_configuration.rb“`

    • Modify the default preference:

      preference :logo, :string, default: 'logo/spree_50.png'

      change spree_50.pngto xxx.png

  3. Decorator design pattern

    Add a decorative file:
    app/models/spree/app_configuration_decorator.rb

    then, add xxx.png as your logo image.

Spree::AppConfiguration.class_eval do
  preference :logo, :string, default: 'logo/xxx.png'
end

The decorator design pattern is a better solution for changing the default logo for Spree site.

Deployment

bundle exec rake railties:install:migrations
bundle exec rake db:migrate
bundle exec rake db:seed
bundle exec rake spree_sample:load

Demo Site

The demo site:
- Demoshop
- Admin page, login as: admin@test.com/spree123

The code base is here

Monday, July 10, 2017

Upgrade Rails From 4.2 to 5.0

Upgrade Rails From 4.2 to 5.0

Please see the reference: Upgrading from Rails 4.2 to Rails 5.0

Ruby version > 2.2.2

  $ rvm get stable
  $ rvm install 5.1.2
  $ rvm --default use 5.1.2 (use 5.1.2 as default and current version)
  $ rvm list

Modify Gemfile

  gem 'rails', '4.2.5.1' -> '5.1.2'
  gem 'coffee-rails'     -> # , '~> 4.1.0' 

Active Record Models Now Inherit from ApplicationRecord by Default.

Create an application_record.rb file in  ```app/models/``` and add the following content:
    class ApplicationRecord < ActiveRecord::Base
      self.abstract_class = true
    end

Then modify all model as:

    class Post < ApplicationRecord
                :
    end

Comment the following line in config/application.rb

#config.active_record.raise_in_transactional_callbacks = true

Change ‘for’ to ‘permit’ in ‘controllers/application_controller.rb’ (if gem devise is used)

class ApplicationController < ActionController::Base
  :
  :
  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:name, :email, :password, :password_confirmation)}
    devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:name, :email, :password, :password_confirmation, :current_password) }
  end
end

Update Database Migration File

It is necessary to add version information after version information in database migration file after Rails 5. For example, [5.0]is added in the following example:

class CreateOrders < ActiveRecord::Migration[5.0]
  def change
    create_table :orders do |t|
      t.string :po_number
      t.date :shipment_require_date
      t.date :order_date
      t.string :ship_to
      t.text :reference

      t.timestamps
    end
  end
end

Or, we can add a bunch of code to test which version of Rails and add the information to the end of class declaration. Likewise:

migration_superclass = if ActiveRecord::VERSION::MAJOR >= 5
  ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"]
else
  ActiveRecord::Migration
end

And also change ActiveRecord::Migration to migration_superclass.

class CreateOrders < migration_superclass

Update gem spring

System shows warning message while execute bundle install:

Array values in the parameter to `Gem.paths=` are deprecated.
Please use a String or nil.

The issue has been posted here.

The solution is to update gem spring:

bundle update spring && bundle exec spring binstub --remove --all && bundle exec spring binstub --all

Git Reset to the Certain Commit

Git Reset to the Certain Commit

“git revert”

If I need to delete the change after the last commit, however, it has been committed and pushed to the remote repository. How can I do ?

If you just want to revert to the last commit, the command git revert can recover the change.

git revert HEAD

But in this way, the log would record the revert history in the log as follows:

$ git log
commit 7bcf5e3b6fc47e875ec226ce2b13a53df73cf626
Author: yourname <yourname@yourmail.com>
Date:   Wed Jul 8 15:46:28 2017 +0900

    Revert "a certain change"

    This reverts commit 0d4a808c26908cd5fe4b6294a00150342d1a58be.

commit 0d4a808c26908cd5fe4b6294a00150342d1a58be
Author: yourname <yourname@yourmail.com>
Date:   Mon Jul 6 23:19:26 2017 +0900

    a certain change

Back to a certain commit

If I need to move back to a certain commit ( for example 4 commits ), I can do as follows:

First, git reset to the commit with its SHA value. It would move back to that commit. For example:

git reset a8b5a0afea1e1f5faccda4a698c0002bdcc7bf892

The commit is back to that point, but not the content. The commandgit status would let yo know the files changed during this period of time.

And then:

git checkout -f

The contents will be moved back to the status of that commit.

Finally,

git reset origin/master

Point the commit to the latest position and push. The remote repository will move back to that commit and the change history (commit log) remains.