Class: Databound::Manager
- Inherits:
-
Object
- Object
- Databound::Manager
- Defined in:
- lib/databound/manager.rb
Instance Method Summary collapse
- #create_from_data ⇒ Object
- #destroy_from_data ⇒ Object
- #find_scoped_records(only_extra_scopes: false) ⇒ Object
-
#initialize(controller) ⇒ Manager
constructor
A new instance of Manager.
- #update_from_data ⇒ Object
Constructor Details
#initialize(controller) ⇒ Manager
Returns a new instance of Manager.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/databound/manager.rb', line 4 def initialize(controller) @model = controller.send(:model) @permitted_columns = controller.send(:permitted_columns) scope_js = controller.params[:scope] data_js = controller.params[:data] extra_where_scopes_js = controller.params[:extra_where_scopes] || '[]' @scope = Databound::Data.new(controller, scope_js) @data = Databound::Data.new(controller, data_js).to_h @extra_where_scopes = JSON.parse(extra_where_scopes_js).map do |extra_scope| Databound::Data.new(controller, extra_scope) end end |
Instance Method Details
#create_from_data ⇒ Object
35 36 37 38 |
# File 'lib/databound/manager.rb', line 35 def create_from_data check_params! @model.where(@scope.to_h).create(@data) end |
#destroy_from_data ⇒ Object
50 51 52 |
# File 'lib/databound/manager.rb', line 50 def destroy_from_data @model.find(@data['id']).destroy end |
#find_scoped_records(only_extra_scopes: false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/databound/manager.rb', line 20 def find_scoped_records(only_extra_scopes: false) records = [] records << @scope.records(@model) @extra_where_scopes.each do |extra_scope| records << extra_scope.records(@model) end if only_extra_scopes records.flatten else records.map { |record| record.where(@data) }.flatten end end |
#update_from_data ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/databound/manager.rb', line 40 def update_from_data id = @data.delete('id') check_params! record = @model.find(id) record.update(@data) record end |