From 090785aa687bf83420287d6c0590ee7dc1477e66 Mon Sep 17 00:00:00 2001 From: Anthony Persaud Date: Mon, 3 May 2021 17:57:24 -0700 Subject: [PATCH] Bump 1.10.1, removes dependency on faraday_middleware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#62] Separate positional and keyword arguments for Ruby 3.0 Replaces deprecated Proc.new usage “warning: Capturing the given block using Proc.new is deprecated; use `&block` instead” Remove upper bound for dependencies versions Remove extra comma remove dependence on AMS ignore leading underscore Updated dependencies Updates version + dependencies ruby 3.0 compatibility fix block param more ruby 3 compatibility faraday 2 compatibility Remove models overrides templates Can't have two autoloaded files defining the same class. It results in inconsistent behavior. Built-in parse classes have to be properly reopened with class_eval. compatibility with Rails strong parameters update doc with reopening builtin classes Removing dependencies for faraday_middleware Bumping to 1.10.1 Bumping to 1.10.1 --- .idea/.gitignore | 8 ++ .idea/misc.xml | 4 + .idea/modules.xml | 8 ++ .idea/parse-stack.iml | 34 +++++++ .idea/vcs.xml | 6 ++ .solargraph.yml | 1 - Gemfile.lock | 95 +++++++++---------- README.md | 33 +++++++ lib/parse/api/users.rb | 4 +- lib/parse/client.rb | 13 ++- lib/parse/client/authentication.rb | 1 - lib/parse/client/batch.rb | 8 +- lib/parse/client/body_builder.rb | 3 +- lib/parse/client/caching.rb | 1 - lib/parse/client/response.rb | 4 +- .../model/associations/collection_proxy.rb | 20 ++-- lib/parse/model/associations/has_many.rb | 2 +- .../associations/relation_collection_proxy.rb | 4 +- lib/parse/model/classes/session.rb | 2 +- lib/parse/model/classes/user.rb | 4 +- lib/parse/model/core/actions.rb | 8 +- lib/parse/model/core/builder.rb | 4 + lib/parse/model/core/fetching.rb | 2 +- lib/parse/model/core/properties.rb | 4 +- lib/parse/model/core/querying.rb | 4 +- lib/parse/model/date.rb | 2 +- lib/parse/model/model.rb | 2 +- lib/parse/model/object.rb | 9 +- lib/parse/model/pointer.rb | 2 +- lib/parse/model/push.rb | 2 +- lib/parse/query.rb | 34 +++---- lib/parse/stack/generators/rails.rb | 10 +- lib/parse/stack/tasks.rb | 2 +- lib/parse/stack/version.rb | 2 +- lib/parse/webhooks.rb | 19 ++-- lib/parse/webhooks/payload.rb | 4 +- parse-stack.gemspec | 15 ++- test/lib/parse/models/attributes_test.rb | 34 +++++++ 38 files changed, 269 insertions(+), 145 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/parse-stack.iml create mode 100644 .idea/vcs.xml create mode 100644 test/lib/parse/models/attributes_test.rb diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c67142b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3250f12 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/parse-stack.iml b/.idea/parse-stack.iml new file mode 100644 index 0000000..76f5e61 --- /dev/null +++ b/.idea/parse-stack.iml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.solargraph.yml b/.solargraph.yml index b4315bc..c16e427 100644 --- a/.solargraph.yml +++ b/.solargraph.yml @@ -15,7 +15,6 @@ max_files: 5000 require: - activemodel - faraday - - faraday_middleware - moneta - activesupport - rack diff --git a/Gemfile.lock b/Gemfile.lock index 925dc77..dac6fd6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,28 +1,27 @@ PATH remote: . specs: - parse-stack (1.9.1) - active_model_serializers (>= 0.9, < 1) - activemodel (>= 5, < 7) - activesupport (>= 5, < 7) - faraday (< 1) - faraday_middleware (>= 0.9, < 2) - moneta (< 2) - parallel (>= 1.6, < 2) - rack (>= 2.0.6, < 3) + parse-stack (1.10.1) + active_model_serializers (>= 0.9) + activemodel (>= 5) + activesupport (>= 5) + faraday + moneta + parallel (>= 1.6) + rack (>= 2.0.6) GEM remote: https://rubygems.org/ specs: - actionpack (6.1.7.1) - actionview (= 6.1.7.1) - activesupport (= 6.1.7.1) - rack (~> 2.0, >= 2.0.9) + actionpack (7.0.5) + actionview (= 7.0.5) + activesupport (= 7.0.5) + rack (~> 2.0, >= 2.2.4) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) - actionview (6.1.7.1) - activesupport (= 6.1.7.1) + actionview (7.0.5) + activesupport (= 7.0.5) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) @@ -32,14 +31,13 @@ GEM activemodel (>= 4.1, < 7.1) case_transform (>= 0.2) jsonapi-renderer (>= 0.1.1.beta1, < 0.3) - activemodel (6.1.7.1) - activesupport (= 6.1.7.1) - activesupport (6.1.7.1) + activemodel (7.0.5) + activesupport (= 7.0.5) + activesupport (7.0.5) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) - zeitwerk (~> 2.3) ansi (1.5.0) binding_of_caller (1.0.0) debug_inspector (>= 0.0.1) @@ -48,38 +46,39 @@ GEM case_transform (0.2) activesupport coderay (1.1.3) - concurrent-ruby (1.1.10) - connection_pool (2.3.0) + concurrent-ruby (1.2.2) + connection_pool (2.4.1) crass (1.0.6) daemons (1.4.1) debug_inspector (1.1.0) dotenv (2.8.1) erubi (1.12.0) eventmachine (1.2.7) - faraday (0.17.6) - multipart-post (>= 1.2, < 3) - faraday_middleware (0.14.0) - faraday (>= 0.7.4, < 1.0) - i18n (1.12.0) + faraday (2.7.5) + faraday-net_http (>= 2.0, < 3.1) + ruby2_keywords (>= 0.0.4) + faraday-net_http (3.0.2) + i18n (1.13.0) concurrent-ruby (~> 1.0) jsonapi-renderer (0.2.2) - loofah (2.19.1) + loofah (2.21.3) crass (~> 1.0.2) - nokogiri (>= 1.5.9) + nokogiri (>= 1.12.0) method_source (1.0.0) - mini_portile2 (2.8.1) - minitest (5.17.0) - minitest-reporters (1.5.0) + mini_portile2 (2.8.2) + minitest (5.18.0) + minitest-reporters (1.6.0) ansi builder minitest (>= 5.0) ruby-progressbar - moneta (1.5.2) - multipart-post (2.2.3) - nokogiri (1.13.10) - mini_portile2 (~> 2.8.0) + moneta (1.6.0) + nokogiri (1.15.2) + mini_portile2 (~> 2.8.2) racc (~> 1.4) - parallel (1.22.1) + nokogiri (1.15.2-x86_64-darwin) + racc (~> 1.4) + parallel (1.23.0) pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -89,8 +88,8 @@ GEM binding_of_caller (~> 1.0) pry (~> 0.13) racc (1.6.2) - rack (2.2.6.2) - rack-test (2.0.2) + rack (2.2.7) + rack-test (2.1.0) rack (>= 1.3) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) @@ -98,26 +97,26 @@ GEM rails-html-sanitizer (1.5.0) loofah (~> 2.19, >= 2.19.1) rake (13.0.6) - redcarpet (3.5.1) + redcarpet (3.6.0) redis (5.0.6) redis-client (>= 0.9.0) - redis-client (0.12.1) + redis-client (0.14.1) connection_pool - ruby-progressbar (1.11.0) - rufo (0.13.0) - thin (1.8.1) + ruby-progressbar (1.13.0) + ruby2_keywords (0.0.5) + rufo (0.16.1) + thin (1.8.2) daemons (~> 1.0, >= 1.0.9) eventmachine (~> 1.0, >= 1.0.4) rack (>= 1, < 3) - tzinfo (2.0.5) + tzinfo (2.0.6) concurrent-ruby (~> 1.0) - webrick (1.7.0) - yard (0.9.28) - webrick (~> 1.7.0) - zeitwerk (2.6.6) + yard (0.9.34) PLATFORMS ruby + x86_64-darwin-20 + x86_64-darwin-22 DEPENDENCIES byebug diff --git a/README.md b/README.md index 2e84633..b11ff20 100644 --- a/README.md +++ b/README.md @@ -647,6 +647,39 @@ data.acl # => ACL({"role:Admin"=>{"read"=>true, "write"=>true}}) For more information about Parse record ACLs, see the documentation at [Security](http://docs.parseplatform.org/rest/guide/#security) +## Builtin parse collections + +These classes match parse builtin collections. Do not redeclare them as rails autoloader don't support multiple class declarations. To add properties and methods to these classes, properly reopen them with overrides, using this class_eval method. + +First, append this to config/application.rb + +```ruby +## +# Don't autoload overrides but preload them instead + overrides = "#{Rails.root}/app/overrides" + Rails.autoloaders.main.ignore(overrides) + + config.to_prepare do + Dir.glob("#{overrides}/**/*_override.rb").each do |override| + load override + end + end +``` + +Then, create the folder app/overrides/models. Create a file named [collection]_override.rb in this folder to reopen one of these classes. Example with the _User collection: + +```ruby +# app/overrides/models/user_override.rb +Parse::User.class_eval do + has_many :created_articles, as: :articles, field: :creator + + property :first_name + property :last_name + property :picture +end +``` + + ### [Parse::Session](https://www.modernistik.com/gems/parse-stack/Parse/Session.html) This class represents the data and columns contained in the standard Parse `_Session` collection. You may add additional properties and methods to this class. See [Session API Reference](https://www.modernistik.com/gems/parse-stack/Parse/Session.html). You may call `Parse.use_shortnames!` to use `Session` in addition to `Parse::Session`. diff --git a/lib/parse/api/users.rb b/lib/parse/api/users.rb index bba45d0..295df95 100644 --- a/lib/parse/api/users.rb +++ b/lib/parse/api/users.rb @@ -84,7 +84,7 @@ def update_user(id, body = {}, headers: {}, **opts) # @return [Parse::Response] def set_service_auth_data(id, service_name, auth_data, headers: {}, **opts) body = { authData: { service_name => auth_data } } - update_user(id, body, opts) + update_user(id, body, **opts) end # Delete a {Parse::User} record given an objectId. @@ -143,7 +143,7 @@ def logout(session_token, headers: {}, **opts) def signup(username, password, email = nil, body: {}, **opts) body = body.merge({ username: username, password: password }) body[:email] = email || body[:email] - create_user(body, opts) + create_user(body, **opts) end end # Users end #API diff --git a/lib/parse/client.rb b/lib/parse/client.rb index 167cc9d..06be42a 100644 --- a/lib/parse/client.rb +++ b/lib/parse/client.rb @@ -1,8 +1,7 @@ require "faraday" -require "faraday_middleware" require "active_support" require "moneta" -require "active_model_serializers" + require "active_support/inflector" require "active_support/core_ext/object" require "active_support/core_ext/string" @@ -202,8 +201,8 @@ def client(conn = :default) # @see Parse::Middleware::Caching # @see Parse::Middleware::Authentication # @see Parse::Protocol - def setup(opts = {}) - @clients[:default] = self.new(opts, &Proc.new) + def setup(opts = {}, &block) + @clients[:default] = self.new(opts, &block) end end @@ -481,7 +480,7 @@ def request(method, uri = nil, body: nil, query: nil, headers: nil, opts: {}) retry end raise - rescue Faraday::Error::ClientError, Net::OpenTimeout => e + rescue Faraday::ClientError, Net::OpenTimeout => e if _retry_count > 0 warn "[Parse:Retry] Retries remaining #{_retry_count} : #{_request}" _retry_count -= 1 @@ -579,9 +578,9 @@ def client # @yield (see Parse::Client.setup) # @return (see Parse::Client.setup) # @see Parse::Client.setup - def self.setup(opts = {}) + def self.setup(opts = {}, &block) if block_given? - Parse::Client.new(opts, &Proc.new) + Parse::Client.new(opts, &block) else Parse::Client.new(opts) end diff --git a/lib/parse/client/authentication.rb b/lib/parse/client/authentication.rb index afcd902..30efaf1 100644 --- a/lib/parse/client/authentication.rb +++ b/lib/parse/client/authentication.rb @@ -2,7 +2,6 @@ # frozen_string_literal: true require "faraday" -require "faraday_middleware" require "active_support" require "active_support/core_ext" diff --git a/lib/parse/client/batch.rb b/lib/parse/client/batch.rb index de696e5..7c04132 100644 --- a/lib/parse/client/batch.rb +++ b/lib/parse/client/batch.rb @@ -85,9 +85,9 @@ def change_requests end # @return [Array] - def each + def each(&block) return enum_for(:each) unless block_given? - @requests.each(&Proc.new) + @requests.each(&block) end # @return [Hash] a formatted payload for the batch request. @@ -125,7 +125,7 @@ def error? # @param segment [Integer] the number of requests to send in each batch. Default 50. # @return [Array] the corresponding set of responses for # each request in the batch. - def submit(segment = 50) + def submit(segment = 50, &block) @responses = [] @requests.uniq!(&:signature) @responses = @requests.each_slice(segment).to_a.threaded_map(2) do |slice| @@ -133,7 +133,7 @@ def submit(segment = 50) end @responses.flatten! #puts "Requests: #{@requests.count} == Response: #{@responses.count}" - @requests.zip(@responses).each(&Proc.new) if block_given? + @requests.zip(@responses).each(&block) if block_given? @responses end diff --git a/lib/parse/client/body_builder.rb b/lib/parse/client/body_builder.rb index 94037b8..b6586d0 100644 --- a/lib/parse/client/body_builder.rb +++ b/lib/parse/client/body_builder.rb @@ -2,12 +2,11 @@ # frozen_string_literal: true require "faraday" -require "faraday_middleware" require_relative "response" require_relative "protocol" require "active_support" require "active_support/core_ext" -require "active_model_serializers" + module Parse diff --git a/lib/parse/client/caching.rb b/lib/parse/client/caching.rb index d9b9337..64b8278 100644 --- a/lib/parse/client/caching.rb +++ b/lib/parse/client/caching.rb @@ -2,7 +2,6 @@ # frozen_string_literal: true require "faraday" -require "faraday_middleware" require "moneta" require_relative "protocol" diff --git a/lib/parse/client/response.rb b/lib/parse/client/response.rb index 1f6a3ee..161f005 100644 --- a/lib/parse/client/response.rb +++ b/lib/parse/client/response.rb @@ -156,9 +156,9 @@ def first # Iterate through each result item. # @yieldparam [Object] a result entry. - def each + def each(&block) return enum_for(:each) unless block_given? - results.each(&Proc.new) + results.each(&block) self end diff --git a/lib/parse/model/associations/collection_proxy.rb b/lib/parse/model/associations/collection_proxy.rb index 6b56529..561614a 100644 --- a/lib/parse/model/associations/collection_proxy.rb +++ b/lib/parse/model/associations/collection_proxy.rb @@ -328,33 +328,33 @@ def notify_will_change! end # Alias for Array#each - def each + def each(&block) return collection.enum_for(:each) unless block_given? - collection.each &Proc.new + collection.each(&block) end # Alias for Array#map - def map + def map(&block) return collection.enum_for(:map) unless block_given? - collection.map &Proc.new + collection.map(&block) end # Alias for Array#select - def select + def select(&block) return collection.enum_for(:select) unless block_given? - collection.select &Proc.new + collection.select(&block) end # Alias for Array#uniq - def uniq - return collection.uniq(&Proc.new) if block_given? + def uniq(&block) + return collection.uniq(&block) if block_given? return collection.uniq end # Alias for Array#uniq! - def uniq! + def uniq!(&block) notify_will_change! - return collection.uniq!(&Proc.new) if block_given? + return collection.uniq!(&block) if block_given? return collection.uniq! end diff --git a/lib/parse/model/associations/has_many.rb b/lib/parse/model/associations/has_many.rb index 6d69020..86d1e4d 100644 --- a/lib/parse/model/associations/has_many.rb +++ b/lib/parse/model/associations/has_many.rb @@ -406,7 +406,7 @@ def has_many(key, scope = nil, **opts) opts[:through] ||= :query if opts[:through] == :query - return has_many_queried(key, scope, opts) + return has_many_queried(key, scope, **opts) end # below this is the same diff --git a/lib/parse/model/associations/relation_collection_proxy.rb b/lib/parse/model/associations/relation_collection_proxy.rb index 41d31c2..2fb719c 100644 --- a/lib/parse/model/associations/relation_collection_proxy.rb +++ b/lib/parse/model/associations/relation_collection_proxy.rb @@ -53,11 +53,11 @@ def initialize(collection = nil, delegate: nil, key: nil, parse_class: nil) # You can get items within the collection relation filtered by a specific set # of query constraints. - def all(constraints = {}) + def all(constraints = {}, &block) q = query({ limit: :max }.merge(constraints)) if block_given? # if we have a query, then use the Proc with it (more efficient) - return q.present? ? q.results(&Proc.new) : collection.each(&Proc.new) + return q.present? ? q.results(&block) : collection.each(&block) end # if no block given, get all the results q.present? ? q.results : collection diff --git a/lib/parse/model/classes/session.rb b/lib/parse/model/classes/session.rb index 36db1f7..aa5cef8 100644 --- a/lib/parse/model/classes/session.rb +++ b/lib/parse/model/classes/session.rb @@ -68,7 +68,7 @@ class Session < Parse::Object # @param token [String] the session token # @return [Session] the session for this token, otherwise nil. def self.session(token, **opts) - response = client.fetch_session(token, opts) + response = client.fetch_session(token, **opts) if response.success? return Parse::Session.build response.result end diff --git a/lib/parse/model/classes/user.rb b/lib/parse/model/classes/user.rb index 19b3051..c3f90c8 100644 --- a/lib/parse/model/classes/user.rb +++ b/lib/parse/model/classes/user.rb @@ -29,7 +29,7 @@ class InvalidEmailAddress < Error; end # The main class representing the _User table in Parse. A user can either be signed up or anonymous. # All users need to have a username and a password, with email being optional but globally unique if set. - # You may add additional properties by redeclaring the class to match your specific schema. + # You may add additional properties by reopening the class to match your specific schema. # # The default schema for the {User} class is as follows: # @@ -417,7 +417,7 @@ def self.session(token, opts = {}) def self.session!(token, opts = {}) # support Parse::Session objects token = token.session_token if token.respond_to?(:session_token) - response = client.current_user(token, opts) + response = client.current_user(token, **opts) response.success? ? Parse::User.build(response.result) : nil end diff --git a/lib/parse/model/core/actions.rb b/lib/parse/model/core/actions.rb index 8b62384..6840f96 100644 --- a/lib/parse/model/core/actions.rb +++ b/lib/parse/model/core/actions.rb @@ -15,9 +15,9 @@ class Query # Supporting the `all` class method to be used in scope chaining with queries. # @!visibility private - def all(expressions = { limit: :max }) + def all(expressions = { limit: :max }, &block) conditions(expressions) - return results(&Proc.new) if block_given? + return results(&block) if block_given? results end @@ -35,7 +35,7 @@ def first_or_create(query_attrs = {}, resource_attrs = {}) # Supporting the `save_all` method to be used in scope chaining with queries. # @!visibility private - def save_all(expressions = {}) + def save_all(expressions = {}, &block) conditions(expressions) klass = Parse::Model.find_class self.table if klass.blank? @@ -43,7 +43,7 @@ def save_all(expressions = {}) end hash_constraints = constraints(true) - klass.save_all(hash_constraints, &Proc.new) if block_given? + klass.save_all(hash_constraints, &block) if block_given? klass.save_all(hash_constraints) end end diff --git a/lib/parse/model/core/builder.rb b/lib/parse/model/core/builder.rb index e95147c..8c72b8c 100644 --- a/lib/parse/model/core/builder.rb +++ b/lib/parse/model/core/builder.rb @@ -43,6 +43,10 @@ def self.build!(schema) raise ArgumentError, "No valid className provided for schema hash" end + # Remove leading underscore, as ruby constants have to start with an uppercase letter + + className = className[1..] if className[0] == '_' + begin klass = Parse::Model.find_class className klass = ::Object.const_get(className.to_parse_class) if klass.nil? diff --git a/lib/parse/model/core/fetching.rb b/lib/parse/model/core/fetching.rb index 8c69c90..448cc68 100644 --- a/lib/parse/model/core/fetching.rb +++ b/lib/parse/model/core/fetching.rb @@ -15,7 +15,7 @@ module Fetching # @param opts [Hash] a set of options to pass to the client request. # @return [self] the current object, useful for chaining. def fetch!(opts = {}) - response = client.fetch_object(parse_class, id, opts) + response = client.fetch_object(parse_class, id, **opts) if response.error? puts "[Fetch Error] #{response.code}: #{response.error}" end diff --git a/lib/parse/model/core/properties.rb b/lib/parse/model/core/properties.rb index 109c37d..42eb49c 100644 --- a/lib/parse/model/core/properties.rb +++ b/lib/parse/model/core/properties.rb @@ -7,9 +7,9 @@ require "active_support/core_ext" require "active_support/core_ext/object" require "active_support/inflector" -require "active_model_serializers" + require "active_support/inflector" -require "active_model_serializers" + require "active_support/hash_with_indifferent_access" require "time" diff --git a/lib/parse/model/core/querying.rb b/lib/parse/model/core/querying.rb index 56598b1..502ff62 100644 --- a/lib/parse/model/core/querying.rb +++ b/lib/parse/model/core/querying.rb @@ -207,10 +207,10 @@ def each(constraints = {}, &block) # by the server. # @return [Array] an array of matching objects. If a block is passed, # an empty array is returned. - def all(constraints = { limit: :max }) + def all(constraints = { limit: :max }, &block) constraints = constraints.reverse_merge({ limit: :max }) prepared_query = query(constraints) - return prepared_query.results(&Proc.new) if block_given? + return prepared_query.results(&block) if block_given? prepared_query.results end diff --git a/lib/parse/model/date.rb b/lib/parse/model/date.rb index 0193c4a..6a84c91 100644 --- a/lib/parse/model/date.rb +++ b/lib/parse/model/date.rb @@ -10,7 +10,7 @@ require "active_support/core_ext/date/calculations" require "active_support/core_ext/date_time/calculations" require "active_support/core_ext/time/calculations" -require "active_model_serializers" + require_relative "model" module Parse diff --git a/lib/parse/model/model.rb b/lib/parse/model/model.rb index 33f8ef3..bd79100 100644 --- a/lib/parse/model/model.rb +++ b/lib/parse/model/model.rb @@ -5,7 +5,7 @@ require "active_support" require "active_support/inflector" require "active_support/core_ext/object" -require "active_model_serializers" + require_relative "../client" module Parse diff --git a/lib/parse/model/object.rb b/lib/parse/model/object.rb index 79d249d..3cb0e4b 100644 --- a/lib/parse/model/object.rb +++ b/lib/parse/model/object.rb @@ -7,7 +7,7 @@ require "active_support/core_ext" require "active_support/core_ext/object" require "active_support/core_ext/string" -require "active_model_serializers" + require "time" require "open-uri" @@ -298,11 +298,12 @@ def as_json(opts = nil) def initialize(opts = {}) if opts.is_a?(String) #then it's the objectId @id = opts.to_s - elsif opts.is_a?(Hash) + elsif opts.respond_to?(:to_h) #if the objectId is provided we will consider the object pristine #and not track dirty items - dirty_track = opts[Parse::Model::OBJECT_ID] || opts[:objectId] || opts[:id] - apply_attributes!(opts, dirty_track: !dirty_track) + _attributes = opts.to_h + dirty_track = _attributes[Parse::Model::OBJECT_ID] || _attributes[:objectId] || _attributes[:id] + apply_attributes!(_attributes, dirty_track: !dirty_track) end # if no ACLs, then apply the class default acls diff --git a/lib/parse/model/pointer.rb b/lib/parse/model/pointer.rb index 967b2ea..431ed70 100644 --- a/lib/parse/model/pointer.rb +++ b/lib/parse/model/pointer.rb @@ -5,7 +5,7 @@ require "active_support" require "active_support/inflector" require "active_support/core_ext" -require "active_model_serializers" + require_relative "model" module Parse diff --git a/lib/parse/model/push.rb b/lib/parse/model/push.rb index 1520ab9..d1e831a 100644 --- a/lib/parse/model/push.rb +++ b/lib/parse/model/push.rb @@ -3,7 +3,7 @@ require_relative "../query.rb" require_relative "../client.rb" -require "active_model_serializers" + module Parse # This class represents the API to send push notification to devices that are diff --git a/lib/parse/query.rb b/lib/parse/query.rb index c6e8695..3d21d0e 100644 --- a/lib/parse/query.rb +++ b/lib/parse/query.rb @@ -6,7 +6,7 @@ require_relative "query/constraints" require_relative "query/ordering" require "active_model" -require "active_model_serializers" + require "active_support" require "active_support/inflector" require "active_support/core_ext" @@ -635,7 +635,7 @@ def distinct(field) compile_query[:distinct] = Query.format_field(field).to_sym @count = old_count_value # perform aggregation - return client.aggregate_objects(@table, compile_query.as_json, _opts).result + return client.aggregate_objects(@table, compile_query.as_json, **_opts).result else raise ArgumentError, "Invalid field name passed to `distinct`." end @@ -654,7 +654,7 @@ def distinct(field) def count old_value = @count @count = 1 - res = client.find_objects(@table, compile.as_json, _opts).count + res = client.find_objects(@table, compile.as_json, **_opts).count @count = old_value res end @@ -662,25 +662,25 @@ def count # @yield a block yield for each object in the result # @return [Array] # @see Array#each - def each + def each(&block) return results.enum_for(:each) unless block_given? # Sparkling magic! - results.each(&Proc.new) + results.each(&block) end # @yield a block yield for each object in the result # @return [Array] # @see Array#map - def map + def map(&block) return results.enum_for(:map) unless block_given? # Sparkling magic! - results.map(&Proc.new) + results.map(&block) end # @yield a block yield for each object in the result # @return [Array] # @see Array#select - def select + def select(&block) return results.enum_for(:select) unless block_given? # Sparkling magic! - results.select(&Proc.new) + results.select(&block) end # @return [Array] @@ -700,7 +700,7 @@ def first(limit = 1) # max_results is used to iterate through as many API requests as possible using # :skip and :limit paramter. # @!visibility private - def max_results(raw: false, on_batch: nil, discard_results: false) + def max_results(raw: false, on_batch: nil, discard_results: false, &block) compiled_query = compile batch_size = 1_000 results = [] @@ -725,7 +725,7 @@ def max_results(raw: false, on_batch: nil, discard_results: false) items = decode(items) unless raw # if a block is provided, we do not keep the results after processing. if block_given? - items.each(&Proc.new) + items.each(&block) else # concat results unless discard_results is true results += items unless discard_results @@ -766,7 +766,7 @@ def _opts # @param compiled_query [Hash] the compiled query # @return [Parse::Response] a response for a query request. def fetch!(compiled_query) - response = client.find_objects(@table, compiled_query.as_json, _opts) + response = client.find_objects(@table, compiled_query.as_json, **_opts) if response.error? puts "[ParseQuery] #{response.error}" end @@ -796,15 +796,15 @@ def fetch!(compiled_query) # @yield a block to iterate for each object that matched the query. # @return [Array] if raw is set to true, a set of Parse JSON hashes. # @return [Array] if raw is set to false, a list of matching Parse::Object subclasses. - def results(raw: false) + def results(raw: false, &block) if @results.nil? if block_given? - max_results(raw: raw, &Proc.new) + max_results(raw: raw, &block) elsif @limit.is_a?(Numeric) response = fetch!(compile) return [] if response.error? items = raw ? response.results : decode(response.results) - return items.each(&Proc.new) if block_given? + return items.each(&block) if block_given? @results = items else @results = max_results(raw: raw) @@ -822,9 +822,9 @@ def results(raw: false) # @return [Array] if raw is set to true, a set of Parse JSON hashes. # @return [Array] if raw is set to false, a list of matching Parse::Object subclasses. # @see #results - def all(expressions = { limit: :max }) + def all(expressions = { limit: :max }, &block) conditions(expressions) - return results(&Proc.new) if block_given? + return results(&block) if block_given? results end diff --git a/lib/parse/stack/generators/rails.rb b/lib/parse/stack/generators/rails.rb index 858bcc2..b435402 100644 --- a/lib/parse/stack/generators/rails.rb +++ b/lib/parse/stack/generators/rails.rb @@ -16,11 +16,11 @@ class InstallGenerator < Rails::Generators::Base # @!visibility private def generate_initializer copy_file "parse.rb", "config/initializers/parse.rb" - copy_file "model_user.rb", File.join("app/models", "user.rb") - copy_file "model_role.rb", File.join("app/models", "role.rb") - copy_file "model_session.rb", File.join("app/models", "session.rb") - copy_file "model_installation.rb", File.join("app/models", "installation.rb") - copy_file "webhooks.rb", File.join("app/models", "webhooks.rb") + #copy_file "model_user.rb", File.join("app/models", "user.rb") + #copy_file "model_role.rb", File.join("app/models", "role.rb") + #copy_file "model_session.rb", File.join("app/models", "session.rb") + #copy_file "model_installation.rb", File.join("app/models", "installation.rb") + #copy_file "webhooks.rb", File.join("app/models", "webhooks.rb") end end diff --git a/lib/parse/stack/tasks.rb b/lib/parse/stack/tasks.rb index e3473aa..5bc1142 100644 --- a/lib/parse/stack/tasks.rb +++ b/lib/parse/stack/tasks.rb @@ -117,7 +117,7 @@ def install_tasks task :triggers => :verify_env do endpoint = ENV["HOOKS_URL"] - Parse::Webhooks.register_triggers!(endpoint, { include_wildcard: true }) do |trigger, name| + Parse::Webhooks.register_triggers!(endpoint, include_wildcard: true) do |trigger, name| puts "[+] #{trigger.to_s.ljust(12, " ")} - #{name}" end end diff --git a/lib/parse/stack/version.rb b/lib/parse/stack/version.rb index 9f9a41a..6e0859c 100644 --- a/lib/parse/stack/version.rb +++ b/lib/parse/stack/version.rb @@ -6,6 +6,6 @@ module Parse # The Parse Server SDK for Ruby module Stack # The current version. - VERSION = "1.9.1" + VERSION = "1.10.1" end end diff --git a/lib/parse/webhooks.rb b/lib/parse/webhooks.rb index 8bb2a4d..a14e942 100644 --- a/lib/parse/webhooks.rb +++ b/lib/parse/webhooks.rb @@ -6,7 +6,7 @@ require "active_support/inflector" require "active_support/core_ext/object" require "active_support/core_ext" -require "active_model_serializers" + require "rack" require_relative "client" require_relative "stack" @@ -53,18 +53,18 @@ def self.webhook_function(functionName, block = nil) # @yield the body of the function to be evaluated in the scope of a {Parse::Webhooks::Payload} instance. # @param block [Symbol] the name of the method to call, if no block is passed. # @return (see Parse::Webhooks.route) - def self.webhook(type, block = nil) + def self.webhook(type, function=nil, &block) if type == :function - unless block.is_a?(String) || block.is_a?(Symbol) - raise ArgumentError, "Invalid Cloud Code function name: #{block}" + unless function.is_a?(String) || function.is_a?(Symbol) + raise ArgumentError, "Invalid Cloud Code function name: #{function}" end - Parse::Webhooks.route(:function, block, &Proc.new) + Parse::Webhooks.route(:function, function, block) # then block must be a symbol or a string else - if block_given? - Parse::Webhooks.route(type, self, &Proc.new) - else + if block Parse::Webhooks.route(type, self, block) + else + Parse::Webhooks.route(type, self, function) end end #if block @@ -125,13 +125,12 @@ def routes # name to register with Parse server. # @yield the block that will handle of the webhook trigger or function. # @return (see routes) - def route(type, className, block = nil) + def route(type, className, &block) type = type.to_s.underscore.to_sym #support camelcase if type != :function && className.respond_to?(:parse_class) className = className.parse_class end className = className.to_s - block = Proc.new if block_given? if routes[type].nil? || block.respond_to?(:call) == false raise ArgumentError, "Invalid Webhook registration trigger #{type} #{className}" end diff --git a/lib/parse/webhooks/payload.rb b/lib/parse/webhooks/payload.rb index 25f17cc..a18d1f2 100644 --- a/lib/parse/webhooks/payload.rb +++ b/lib/parse/webhooks/payload.rb @@ -7,7 +7,7 @@ require "active_support/core_ext/object" require "active_support/core_ext/string" require "active_support/core_ext" -require "active_model_serializers" + module Parse class Webhooks @@ -125,7 +125,7 @@ def parse_class def parse_id return nil unless @object.present? @object[Parse::Model::OBJECT_ID] || @object[:objectId] - end; + end; alias_method :objectId, :parse_id # true if this is a webhook trigger request. diff --git a/parse-stack.gemspec b/parse-stack.gemspec index 1a12b6c..32948df 100644 --- a/parse-stack.gemspec +++ b/parse-stack.gemspec @@ -27,14 +27,13 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.required_ruby_version = ">= 2.6" - spec.add_runtime_dependency "activemodel", [">= 5", "< 7"] - spec.add_runtime_dependency "active_model_serializers", [">= 0.9", "< 1"] - spec.add_runtime_dependency "activesupport", [">= 5", "< 7"] - spec.add_runtime_dependency "parallel", [">= 1.6", "< 2"] - spec.add_runtime_dependency "faraday", "< 1" - spec.add_runtime_dependency "faraday_middleware", [">= 0.9", "< 2"] - spec.add_runtime_dependency "moneta", "< 2" - spec.add_runtime_dependency "rack", ">= 2.0.6", "< 3" + spec.add_runtime_dependency "activemodel", [">= 5"] + spec.add_runtime_dependency "active_model_serializers", [">= 0.9"] + spec.add_runtime_dependency "activesupport", [">= 5"] + spec.add_runtime_dependency "parallel", [">= 1.6"] + spec.add_runtime_dependency "faraday" + spec.add_runtime_dependency "moneta" + spec.add_runtime_dependency "rack", ">= 2.0.6" # spec.post_install_message = <