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
- #new ⇒ Object
- #ping ⇒ Object
- #show ⇒ Object
- #tables ⇒ Object
- #update ⇒ Object
Class Method Details
.included(klass) ⇒ Object
4 5 6 7 8 |
# File 'lib/standard_api/controller.rb', line 4 def self.included(klass) 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/standard_api/controller.rb', line 46 def create record = model.new(model_params) instance_variable_set("@#{model.model_name.singular}", record) if record.save if request.format == :html redirect_to record else render :show, status: :created end else if request.format == :html render :edit, status: :bad_request else render :show, status: :bad_request end end end |
#current_mask ⇒ Object
Override if you want to support masking
86 87 88 |
# File 'lib/standard_api/controller.rb', line 86 def current_mask @current_mask ||= {} end |
#destroy ⇒ Object
80 81 82 83 |
# File 'lib/standard_api/controller.rb', line 80 def destroy resources.find(params[:id]).destroy! head :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 |
#new ⇒ Object
42 43 44 |
# File 'lib/standard_api/controller.rb', line 42 def new instance_variable_set("@#{model.model_name.singular}", model.new) if model end |
#ping ⇒ Object
10 11 12 |
# File 'lib/standard_api/controller.rb', line 10 def ping render plain: '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
14 15 16 17 18 19 20 21 |
# File 'lib/standard_api/controller.rb', line 14 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! controllers.map!(&:table_name) render json: controllers end |
#update ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/standard_api/controller.rb', line 65 def update record = resources.find(params[:id]) instance_variable_set("@#{model.model_name.singular}", record) if record.update_attributes(model_params) if request.format == :html redirect_to record else render :show, status: :ok end else render :show, status: :bad_request end end |