diff --git a/Gemfile.lock b/Gemfile.lock index c62d1708..981540d9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,14 +2,14 @@ PATH remote: . specs: parse-stack (1.10.0) - 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) + active_model_serializers (>= 0.9) + activemodel (>= 5) + activesupport (>= 5) + faraday + faraday_middleware (>= 0.9) + moneta + parallel (>= 1.6) + rack (>= 2.0.6) GEM remote: https://rubygems.org/ @@ -154,7 +154,8 @@ GEM zeitwerk (2.4.2) PLATFORMS - ruby + x86_64-darwin-20 + x86_64-linux DEPENDENCIES byebug diff --git a/README.md b/README.md index 2e846338..b11ff20d 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/client.rb b/lib/parse/client.rb index 4384dab6..85065e02 100644 --- a/lib/parse/client.rb +++ b/lib/parse/client.rb @@ -2,7 +2,7 @@ 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" @@ -481,7 +481,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 diff --git a/lib/parse/client/body_builder.rb b/lib/parse/client/body_builder.rb index 94037b80..8ac0c92e 100644 --- a/lib/parse/client/body_builder.rb +++ b/lib/parse/client/body_builder.rb @@ -7,7 +7,7 @@ require_relative "protocol" require "active_support" require "active_support/core_ext" -require "active_model_serializers" + module Parse diff --git a/lib/parse/model/classes/user.rb b/lib/parse/model/classes/user.rb index a15c4379..c3f90c82 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: # diff --git a/lib/parse/model/core/builder.rb b/lib/parse/model/core/builder.rb index e95147c0..8c72b8c9 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/properties.rb b/lib/parse/model/core/properties.rb index 109c37de..42eb49c9 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/date.rb b/lib/parse/model/date.rb index 0193c4ab..6a84c910 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 33f8ef32..bd791001 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 79d249db..3cb0e4bc 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 967b2ea2..431ed709 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 1520ab9d..d1e831af 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 a27b4ccf..3d21d0ec 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" diff --git a/lib/parse/stack/generators/rails.rb b/lib/parse/stack/generators/rails.rb index 858bcc20..b4354028 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 e3473aa9..5bc11421 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/webhooks.rb b/lib/parse/webhooks.rb index 23724ee6..a14e942e 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 diff --git a/lib/parse/webhooks/payload.rb b/lib/parse/webhooks/payload.rb index 25f17ccb..a18d1f2a 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 bfdbd322..ccfb4749 100644 --- a/parse-stack.gemspec +++ b/parse-stack.gemspec @@ -27,14 +27,14 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.required_ruby_version = ">= 2.5.0" - 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 "faraday_middleware", [">= 0.9"] + spec.add_runtime_dependency "moneta" + spec.add_runtime_dependency "rack", ">= 2.0.6" # spec.post_install_message = <