Class: Hai::Read
- Inherits:
-
Object
- Object
- Hai::Read
- Defined in:
- lib/hai/read.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#model ⇒ Object
Returns the value of attribute model.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(model, context) ⇒ Read
constructor
A new instance of Read.
-
#list(filter: nil, limit: nil, offset: nil, sort: nil, **extra) ⇒ Object
return [] or ActiveRecord::Relationship.
-
#read(query_hash) ⇒ Object
return nil or model.
Constructor Details
#initialize(model, context) ⇒ Read
Returns a new instance of Read.
6 7 8 9 10 11 |
# File 'lib/hai/read.rb', line 6 def initialize(model, context) @model = model @context = context @context[:model] = model @table = model.arel_table end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
4 5 6 |
# File 'lib/hai/read.rb', line 4 def context @context end |
#model ⇒ Object
Returns the value of attribute model.
3 4 5 |
# File 'lib/hai/read.rb', line 3 def model @model end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
4 5 6 |
# File 'lib/hai/read.rb', line 4 def table @table end |
Instance Method Details
#list(filter: nil, limit: nil, offset: nil, sort: nil, **extra) ⇒ Object
return [] or ActiveRecord::Relationship
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/hai/read.rb', line 14 def list(filter: nil, limit: nil, offset: nil, sort: nil, **extra) check_list_policy(context) context[:arguments] = extra query = build_filter(filter) query = query.order({ sort.fetch(:field) => sort.fetch(:order) }) if sort query = query.limit(limit) if limit query = query.offset(offset) if offset run_action_modification(query) end |
#read(query_hash) ⇒ Object
return nil or model
26 27 28 29 30 31 32 33 |
# File 'lib/hai/read.rb', line 26 def read(query_hash) build_filter(query_hash).first.tap do |record| if record.respond_to?(:check_hai_policy) && !record.check_hai_policy(:read, context) raise end end end |