module SqlAttributes
Constants
- VERSION
Public Instance Methods
sql_attribute(name, subquery)
click to toggle source
# File lib/sql_attributes.rb, line 29 def sql_attribute(name, subquery) sql_attributes[name] = subquery.squish scope "with_#{name}".to_sym, lambda { select( arel.projections, "(#{subquery.squish}) as #{name}" ) } define_method(name) do return read_attribute(name) if has_attribute?(name) raise NotLoaded, <<~MESSAGE Dynamic SQL attribute `#{name}` not loaded from the database. Use the `with_sql_attributes` scope to load the attribute. MESSAGE end end
sql_attributes()
click to toggle source
# File lib/sql_attributes.rb, line 8 def sql_attributes @sql_attributes ||= HashWithIndifferentAccess.new end
with_sql_attributes(*attributes)
click to toggle source
# File lib/sql_attributes.rb, line 12 def with_sql_attributes(*attributes) requested_attributes = if attributes.length.positive? Array.wrap(attributes).flatten.reject(&:nil?) else sql_attributes.keys end requested_attributes.inject(all) do |scope, name| unless sql_attributes.key?(name) raise NotDefined, "You want to load dynamic SQL attribute `#{name}` but it is not defined." end scope.public_send("with_#{name}") end end