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
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/standard_api/controller.rb', line 27 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
42 43 44 45 |
# File 'lib/standard_api/controller.rb', line 42 def create instance_variable_set("@#{model.model_name.singular}", model.new(model_params)) render :show, status: instance_variable_get("@#{model.model_name.singular}").save ? :created : :bad_request end |
#current_mask ⇒ Object
Override if you want to support masking
58 59 60 |
# File 'lib/standard_api/controller.rb', line 58 def current_mask @current_mask ||= {} end |
#destroy ⇒ Object
52 53 54 55 |
# File 'lib/standard_api/controller.rb', line 52 def destroy resources.find(params[:id]).destroy! render nothing: true, status: :no_content end |
#index ⇒ Object
23 24 25 |
# File 'lib/standard_api/controller.rb', line 23 def index instance_variable_set("@#{model.model_name.plural}", resources.limit(params[:limit]).offset(params[:offset]).sort(orders)) end |
#ping ⇒ Object
11 12 13 |
# File 'lib/standard_api/controller.rb', line 11 def ping render :text => 'pong' end |
#show ⇒ Object
38 39 40 |
# File 'lib/standard_api/controller.rb', line 38 def show instance_variable_set("@#{model.model_name.singular}", resources.find(params[:id])) 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
47 48 49 50 |
# File 'lib/standard_api/controller.rb', line 47 def update instance_variable_set("@#{model.model_name.singular}", resources.find(params[:id])) render :show, status: instance_variable_get("@#{model.model_name.singular}").update_attributes(model_params) ? :ok : :bad_request end |