GithubHelp home page GithubHelp logo

tb / activerecord-database-views Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stevo/activerecord-database-views

0.0 1.0 0.0 212 KB

Allows database views to be stored as sql files and be easily applyable

License: MIT License

activerecord-database-views's Introduction

activerecord-database-views

Allows database views to be stored as sql files and be easily applyable

This is early version of gem - it works, but the code does not look perfect - pull requests are welcome :)

Installation

Just add following line to your Gemfile

gem 'activerecord-database-views'

and run bundle install

Usage

Place your SQL that view will be generated from into db\views directory (The actual view name will be the same as file name), i.e.

-- db\views\reverse_users.sql
SELECT * FROM users ORDER BY users.id DESC

Then run console (console c) and execute...

ActiveRecord::Base.reload_views!

...to reload all defined views.

You can test them out by

ActiveRecord::Base.connection.execute('SELECT * FROM reverse_users').to_a

or just attach them to a model file

class ReverseUser < ActiveRecord::Base
end

and run

ReverseUser.all

How to reload views automatically after migrations

Add new file hooks.rake under lib\tasks and copy following code

db_tasks = %w[db:migrate db:rollback db:schema:load]

namespace :reload_views do
  db_tasks.each do |task_name|
    task task_name => %w[environment db:load_config] do
      ActiveRecord::Base.reload_views!
    end
  end
end

db_tasks.each do |task_name|
  Rake::Task[task_name].enhance do
    Rake::Task["reload_views:#{task_name}"].invoke
  end
end

As a bonus you will be able to reload views using rake task as well, i.e. rake reload_views:db:migrate

activerecord-database-views's People

Contributors

stevo avatar

Watchers

 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.