Module: AppQuery
- Defined in:
- lib/app_query.rb,
lib/app_query/railtie.rb,
lib/app_query/version.rb,
lib/app_query/mappable.rb,
lib/app_query/tokenizer.rb,
lib/app_query/base_query.rb,
lib/app_query/paginatable.rb,
lib/app_query/render_helpers.rb,
lib/generators/app_query/query_generator.rb,
lib/generators/app_query/example_generator.rb
Overview
AppQuery provides a way to work with raw SQL queries using ERB templating, parameter binding, and CTE manipulation.
Defined Under Namespace
Modules: Generators, Mappable, Paginatable, RenderHelpers Classes: BaseQuery, Configuration, Error, Q, Railtie, Result, Tokenizer, UnrenderedQueryError
Constant Summary collapse
- VERSION =
This should just contain the .dev of the upcoming version. When doing the actual release, CI will write the tag here before pushing the gem.
"0.8.0.rc2"
Class Method Summary collapse
-
.[](query_name, **opts) ⇒ Q
Loads a query from a file in the configured query path.
-
.configuration ⇒ Configuration
Returns the current configuration.
-
.configure {|Configuration| ... } ⇒ Object
Yields the configuration for modification.
-
.reset_configuration! ⇒ void
Resets configuration to default values.
-
.table(name, **opts) ⇒ Q
Creates a query that selects all columns from a table.
Class Method Details
.[](query_name, **opts) ⇒ Q
Loads a query from a file in the configured query path.
When no extension is provided, tries .sql first, then .sql.erb.
Raises an error if both files exist (ambiguous).
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/app_query.rb', line 98 def self.[](query_name, **opts) base = Pathname.new(configuration.query_path) / query_name.to_s full_path = if File.extname(query_name.to_s).empty? sql_path = base.sub_ext(".sql"). erb_path = base.sub_ext(".sql.erb"). sql_exists = sql_path.exist? erb_exists = erb_path.exist? if sql_exists && erb_exists raise Error, "Ambiguous query name #{query_name.inspect}: both #{sql_path} and #{erb_path} exist" end sql_exists ? sql_path : erb_path else base. end Q.new(full_path.read, name: "AppQuery #{query_name}", filename: full_path.to_s, **opts) end |
.configuration ⇒ Configuration
Returns the current configuration.
50 51 52 |
# File 'lib/app_query.rb', line 50 def self.configuration @configuration ||= AppQuery::Configuration.new end |
.configure {|Configuration| ... } ⇒ Object
Yields the configuration for modification.
62 63 64 |
# File 'lib/app_query.rb', line 62 def self.configure yield configuration if block_given? end |
.reset_configuration! ⇒ void
This method returns an undefined value.
Resets configuration to default values.
69 70 71 72 73 |
# File 'lib/app_query.rb', line 69 def self.reset_configuration! configure do |config| config.query_path = "app/queries" end end |
.table(name, **opts) ⇒ Q
Creates a query that selects all columns from a table.
Convenience method for quickly querying a table without writing SQL.
134 135 136 137 |
# File 'lib/app_query.rb', line 134 def self.table(name, **opts) quoted = ActiveRecord::Base.connection.quote_table_name(name) Q.new("SELECT * FROM #{quoted}", name: "AppQuery.table(#{name})", **opts) end |