Git Product home page Git Product logo

seo_meta's Introduction

SEO Meta tags plugin

Installation

Add to your project using your Bundler Gemfile:

gem 'seo_meta'

Include in your model by calling the method is_seo_meta in the class declaration, for example with Page:

class Page < ActiveRecord::Base
  is_seo_meta
end

Run the generator:

rails generate seo_meta

Migrate the database (unless you want to read 'Migrating existing data' below first):

rake db:migrate

Migrating existing data

This step is only if you already implemented SEO meta tag fields in your model.

At this point you could add to the migration that is generated in db/migrate/ logic to migrate across your existing data and remove the columns from your model afterward, for example with Page:

class CreateSeoMeta < ActiveRecord::Migration

  def self.up
    # ... migration logic from the seo_meta generator ...

    # Grab the attributes of the records that currently exist
    existing_pages = ::Page.all.map(&:attributes)

    # Remove columns
    ::SeoMeta.attributes.each do |field|
      if ::Page.column_names.map(&:to_sym).include?(field)
        remove_column ::Page.table_name, field
      end
    end

    # Reset column information because otherwise the old columns will still exist.
    ::Page.reset_column_information

    # Re-attach seo_meta
    ::Page.module_eval do
      is_seo_meta
    end

    # Migrate data
    existing_pages.each do |page|
      ::Page.find(page['id']).update_attributes({
        ::SeoMeta.attributes.keys.inject({}) {|attributes, name|
          attributes.merge(name => translation[name.to_s])
        }
      })
    end
  end

  def self.down
    # Grab the attributes of the records that currently exist
    existing_pages = ::Page.all.map(&:attributes)

    # Add columns back to your model
    ::SeoMeta.attributes.each do |field, field_type|
      unless ::Page.column_names.map(&:to_sym).include?(field)
        add_column ::Page.table_name, field, field_type
      end
    end

    # Reset column information because otherwise the new columns won't exist yet.
    ::Page.reset_column_information

    # Migrate data
    existing_pages.each do |page|
      ::Page.find(page['id']).update_attributes({
        ::SeoMeta.attributes.keys.inject({}) {|attributes, name|
          attributes.merge(name => translation[name.to_s])
        }
      })
    end

    # ... migration logic from the seo_meta generator ...
  end

end

Now, run:

rake db:migrate

Form Partial

You can use the included partial if you want a ready made form for the new SEO fields. Note that the :form local variable is required and should be a form builder object from a form_for block, for example:

<%= form_for @page do |f| -%>
  <%= render :partial => '/seo_meta/form', :locals => { :form => f } %>
<% end %>

Anything else?

Nope, all done!

seo_meta's People

Contributors

parndt avatar ugisozols avatar koa avatar mhaylock avatar

Stargazers

Josh Adams avatar

Watchers

Josh Adams avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.