Module: StandardAPI::Controller
- Defined in:
- lib/standard_api/controller.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #calculate ⇒ Object
- #create ⇒ Object
-
#current_mask ⇒ Object
Override if you want to support masking.
- #destroy ⇒ Object
- #index ⇒ Object
- #ping ⇒ Object
- #show ⇒ Object
- #tables ⇒ Object
- #update ⇒ Object
Class Method Details
.included(klass) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/standard_api/controller.rb', line 4 def self.included(klass) klass.hide_action :current_mask klass.helper_method :includes, :orders, :model klass.append_view_path(File.join(File.dirname(__FILE__), 'views')) klass.extend(ClassMethods) end |
Instance Method Details
#calculate ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/standard_api/controller.rb', line 28 def calculate @calculations = resources.reorder(nil).pluck(*calculate_selects).map do |c| if c.is_a?(Array) c.map { |v| v.is_a?(BigDecimal) ? v.to_f : v } else c.is_a?(BigDecimal) ? c.to_f : c end end render json: @calculations end |
#create ⇒ Object
44 45 46 47 48 |
# File 'lib/standard_api/controller.rb', line 44 def create @record = model.new(model_params) instance_variable_set("@#{model.model_name.singular}", @record) render :show, status: @record.save ? :created : :bad_request end |
#current_mask ⇒ Object
Override if you want to support masking
62 63 64 |
# File 'lib/standard_api/controller.rb', line 62 def current_mask @current_mask ||= {} end |
#destroy ⇒ Object
56 57 58 59 |
# File 'lib/standard_api/controller.rb', line 56 def destroy resources.find(params[:id]).destroy! render nothing: true, status: :no_content end |
#index ⇒ Object
23 24 25 26 |
# File 'lib/standard_api/controller.rb', line 23 def index @records = resources.limit(params[:limit]).offset(params[:offset]).sort(orders) instance_variable_set("@#{model.model_name.plural}", @records) end |
#ping ⇒ Object
11 12 13 |
# File 'lib/standard_api/controller.rb', line 11 def ping render :text => 'pong' end |
#show ⇒ Object
39 40 41 42 |
# File 'lib/standard_api/controller.rb', line 39 def show @record = resources.find(params[:id]) instance_variable_set("@#{model.model_name.singular}", @record) end |
#tables ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/standard_api/controller.rb', line 15 def tables controllers = Dir[Rails.root.join('app/controllers/*_controller.rb')].map{ |path| path.match(/(\w+)_controller.rb/)[1].camelize+"Controller" }.map(&:safe_constantize) controllers.select! { |c| c.ancestors.include?(self.class) && c != self.class } controllers.map!(&:model).compact!.map!(&:table_name) render json: controllers end |
#update ⇒ Object
50 51 52 53 54 |
# File 'lib/standard_api/controller.rb', line 50 def update @record = resources.find(params[:id]) instance_variable_set("@#{model.model_name.singular}", @record) render :show, status: @record.update_attributes(model_params) ? :ok : :bad_request end |