Skip to content

Commit

Permalink
Switched support to rails4-only
Browse files Browse the repository at this point in the history
  • Loading branch information
vihai committed Jul 26, 2013
1 parent 3162fac commit b69efb2
Show file tree
Hide file tree
Showing 27 changed files with 220 additions and 219 deletions.
16 changes: 12 additions & 4 deletions lib/active_rest.rb
Expand Up @@ -14,17 +14,25 @@ class Engine < Rails::Engine
end
end

# Rails 3.1 - 3.2
module ActiveRecord::Associations::Builder
class HasMany
self.valid_options += [ :embedded ]
alias_method :ar_valid_options, :valid_options
def valid_options
ar_valid_options + [ :embedded ]
end
end

class HasOne
self.valid_options += [ :embedded, :embedded_in ]
alias_method :ar_valid_options, :valid_options
def valid_options
ar_valid_options + [ :embedded, :embedded_in ]
end
end

class BelongsTo
self.valid_options += [ :embedded, :embedded_in ]
alias_method :ar_valid_options, :valid_options
def valid_options
ar_valid_options + [ :embedded, :embedded_in ]
end
end
end
12 changes: 6 additions & 6 deletions lib/active_rest/controller.rb
Expand Up @@ -212,10 +212,10 @@ def model_symbol
# find a single resource
#
def find_target(opts = {})
@target_relation ||= model.scoped
@target_relation = model.scoped.includes(model.interfaces[:rest].eager_loading_hints(:view => ar_view)) if model
@target_relation ||= model.all
@target_relation = model.all.includes(model.interfaces[:rest].eager_loading_hints(:view => ar_view)) if model

run_callbacks :find_target, action_name do
run_callbacks :find_target do
tid = opts[:id] || params[:id]
opts.delete(:id)

Expand Down Expand Up @@ -246,7 +246,7 @@ def apply_sorting_to_relation(rel)

sorts = params[:sort].split(',')

sorts.each do |sort|
sorts.reverse.each do |sort|
if sort =~ /^([-+]?)(.*)$/
desc = ($1 && $1 == '-')
attrname = $2
Expand Down Expand Up @@ -277,14 +277,14 @@ def apply_pagination_to_relation(rel)
# find all with conditions
#
def find_targets
run_callbacks :find_targets, action_name do
run_callbacks :find_targets do
if params[:_search]
# Fulltext search

@targets = model.search(params[:_search])
@count = @targets.count
else
@targets_relation ||= model.scoped
@targets_relation ||= model.all

# Filters
@targets_relation = apply_scopes_to_relation(@targets_relation)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_rest/controller/filters.rb
Expand Up @@ -72,7 +72,7 @@ def to_arel

if @tree[:field]
# Valid for boolean fields
return @rel.scoped.table[@tree[:field]]
return @rel.table[@tree[:field]]
else
return to_arel_recur(@tree)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/active_rest/controller/validations.rb
Expand Up @@ -59,7 +59,7 @@ def validate_create
end

respond_to do |format|
format.any { render :nothing => true }
format.any { render :nothing => true, :status => :accepted }
end
end

Expand All @@ -82,7 +82,7 @@ def validate_update
end

respond_to do |format|
format.any { render :nothing => true }
format.any { render :nothing => true, :status => :accepted }
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/active_rest/controller/verbs.rb
Expand Up @@ -66,7 +66,7 @@ def schema
end

def index
# @targets_relation = model.scoped.includes(model.interfaces[:rest].eager_loading_hints(:view => ar_view)) if model
# @targets_relation = model.all.includes(model.interfaces[:rest].eager_loading_hints(:view => ar_view)) if model

begin
find_targets
Expand All @@ -84,7 +84,7 @@ def index
ActiveSupport::Inflector.underscore(model.name)).tr('/', '_')
end

respond_with(@targets.kind_of?(ActiveRecord::Relation) ? @targets.all : @targets,
respond_with(@targets,
:total => @count,
:root => root_name) do |format|
yield(format) if block_given?
Expand Down Expand Up @@ -188,7 +188,7 @@ def update
send(ar_transaction_handler) do
before_update

model.ar_apply_update_attributes(:rest, @target, @request_resource, :aaa_context => @aaa_context)
@target.ar_apply_update_attributes(:rest, @request_resource, :aaa_context => @aaa_context)

before_save

Expand All @@ -197,7 +197,7 @@ def update
after_update
end
rescue ActiveRest::Model::Interface::AttributeNotWriteable => e
raise Exception::BadRequest.new(e.message,
raise Exception::UnprocessableEntity.new(e.message,
:errors => { e.attribute_name => [ 'Is not writable' ] },
:retry_possible => false)
rescue ActiveRest::Model::Interface::AttributeNotFound => e
Expand Down
2 changes: 1 addition & 1 deletion lib/active_rest/model.rb
Expand Up @@ -70,7 +70,7 @@ def nested_attribute(attrs, path = [])
attrs = attrs.to_s.split('.') if !attrs.kind_of?(Array)

if attrs.count == 1
attr = self.scoped.table[attrs[0]]
attr = self.all.table[attrs[0]]

if !attr || !self.columns_hash[attrs[0]]
raise UnknownField.new("Unknown field '#{attrs[0]}' in model #{self.name}", attrs[0])
Expand Down
25 changes: 11 additions & 14 deletions lib/active_rest/model/interface.rb
Expand Up @@ -340,19 +340,18 @@ def ar_serializable_hash(obj, opts = {})
capas = []

if opts[:aaa_context]
capasyms = opts[:aaa_context].global_capabilities
capas = opts[:aaa_context].global_capabilities

if obj.respond_to? :capabilities_for
capasyms += obj.capabilities_for(opts[:aaa_context])
capas += obj.capabilities_for(opts[:aaa_context])
end

capas = capabilities.slice(*capasyms)
end

view = opts[:view]

if view.is_a?(Symbol)
view = @views[view]
view = @views[:_default_] if !view
raise ViewNotFound, "View #{opts[:view]} not found" if !view
end

Expand Down Expand Up @@ -381,11 +380,11 @@ def ar_serializable_hash(obj, opts = {})

readable =
attr.readable && # Defined readable in interface
(!authorization_required? || !!capas.map { |k,v| v.readable?(attrname) }.reduce(&:|)) # Authorized to be read
attr_readable?(capas, attrname)

writable =
attr.writable && # Defined writable in interface
(!authorization_required? || !!capas.map { |k,v| v.writable?(attrname) }.reduce(&:|)) # Authorized to be written
attr_writable?(capas, attrname)

if with_perms
perms[attrname] ||= {}
Expand Down Expand Up @@ -500,13 +499,11 @@ def apply_model_attributes(obj, values, opts = {})
capas = []

if opts[:aaa_context]
capasyms = opts[:aaa_context].global_capabilities
capas = opts[:aaa_context].global_capabilities

if obj.respond_to? :capabilities_for
capasyms += obj.capabilities_for(opts[:aaa_context])
capas += obj.capabilities_for(opts[:aaa_context])
end

capas = capabilities.slice(*capasyms)
end

values.each do |valuename, value|
Expand All @@ -528,7 +525,7 @@ def apply_model_attributes(obj, values, opts = {})

writable =
attr.writable &&
!!capas.map { |k,v| v.writable?(valuename) }.reduce(&:|)
attr_writable?(capas, valuename)

raise AttributeNotWriteable.new(obj, valuename) if !writable

Expand Down Expand Up @@ -566,7 +563,7 @@ def apply_model_attributes(obj, values, opts = {})
association.target
else
ids = value.map {|a| a['id'] || a[:id] }.compact
ids.empty? ? [] : association.scoped.where(association.klass.primary_key => ids)
ids.empty? ? [] : association.all.where(association.klass.primary_key => ids)
end

value.each do |attributes|
Expand Down Expand Up @@ -636,11 +633,11 @@ def to_s
end

def attr_readable?(capas, name)
!!(capas.map { |x| @capabilities[x.to_sym].readable?(name) }.reduce(&:|))
!authorization_required? || !!capas.map { |x| @capabilities[x.to_sym].readable?(name) }.reduce(&:|)
end

def attr_writable?(capas, name)
!!(capas.map { |x| @capabilities[x.to_sym].writable?(name) }.reduce(&:|))
!authorization_required? || !!capas.map { |x| @capabilities[x.to_sym].writable?(name) }.reduce(&:|)
end

class AssociatedRecordNotFound < StandardError
Expand Down
2 changes: 1 addition & 1 deletion lib/active_rest/version.rb
@@ -1,3 +1,3 @@
module ActiveRest
VERSION = '5.0.0'
VERSION = '5.1.0'
end
5 changes: 3 additions & 2 deletions specapp/Gemfile
@@ -1,11 +1,12 @@
source 'http://rubygems.org'

gem 'rails', '~> 3.2.0'
#gem 'rails', '~> 3.2.0'
gem 'rails', '~> 4.0.0'

gem 'sqlite3-ruby', :require => 'sqlite3'

gem 'squeel'
gem 'active_rest', '>= 3.0.1', :path => '..'
gem 'active_rest', '>= 5.0.0', :path => '..'

# Bundle gems for certain environments:
group :test, :development do
Expand Down

0 comments on commit b69efb2

Please sign in to comment.