Created
March 6, 2014 14:27
-
-
Save Holek/9390963 to your computer and use it in GitHub Desktop.
ActiveRecord concern for MSSQL views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# | |
# class SomeView < ActiveRecord::Base | |
# include ViewObject | |
# end | |
# | |
# SomeView.view_sql #=> prints out MsSQL view SQL statement | |
module ViewObject | |
extend ActiveSupport::Concern | |
included do | |
self.primary_key = nil | |
end | |
def readonly? | |
true | |
end | |
module ClassMethods | |
def view_sql | |
@view_sql ||= self.connection.select_one("SELECT VIEW_DEFINITION as view_definition FROM INFORMATION_SCHEMA.Views WHERE TABLE_NAME = N'#{self.table_name}'")['view_definition'] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment