devise-activeresource 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a7d274de54921072d3e0982020dac693705a1a221e1e8f009d08447bfd905580
4
+ data.tar.gz: 0c7c4c265c2fdcb27e53d03b6d72cc5b44739d0b954d2ed91f95c9df22e37679
5
+ SHA512:
6
+ metadata.gz: e5104cae5f2f5724ada49676e477bdbb503fbabec125b716d115f482ed6bbd928965b5f48ec7aba86355f644591bcc22cd70acd3cc0b1a2f8171516a1be56db0
7
+ data.tar.gz: 678a4e131143f335900022ab042b09c4d0599aee2b819c7b71eed7999d6afdc40687dddf636d2d2961f59d45b1690507b0b21143df57bba160744dc6df5e4396
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Francisco Caiceo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Devise::Activeresource
2
+ `Devise::Activeresource` is an adapter that allows you to use [devise](https://github.com/heartcombo/devise) with [Active Resource](https://github.com/rails/activeresource) in your models. It supports `Devise >= 4.7.1` and `Active Resource 5.1`. It may work with earlier version of `devise` and `activeresource`, but it's not tested for those versions.
3
+
4
+ ## Usage
5
+ In the `config/inititializer/devise.rb` file, replace the default orm adapter:
6
+ ```ruby
7
+ require 'devise/orm/active_record'
8
+ ```
9
+
10
+ with the following:
11
+ ```ruby
12
+ require 'devise/orm/active_resource'
13
+ ```
14
+
15
+ ### Generators and API
16
+ This gem does not provide model generators because `ActiveResource` models adapt their attributes to the `REST API` you are consuming. These models may have an schema, but this is not enough to ensure proper operation.
17
+
18
+ To ensure the proper functioning of this gem, verify the `REST API` you are consuming, so that it contains the attributes you need for the operation of each module that you include in the model.
19
+
20
+ For more information of the attributes you need for module, look at the [devise documentation](https://github.com/heartcombo/devise/blob/master/README.md).
21
+
22
+ ### Modules
23
+ This gem adds one aditional module to devise:
24
+ * Resource Authenticatable: This is a monkey patch, and it's needed if you want to use the [Database Authenticable module](https://www.rubydoc.info/github/heartcombo/devise/master/Devise/Models/DatabaseAuthenticatable) of `devise`.
25
+
26
+ ### Models
27
+ The options to configure your models are the same of devise. The only consideration, is the `Resource Authenticatable` module. An example of a model:
28
+
29
+ ```ruby
30
+ class User < ActiveResource::Base
31
+ devise :database_authenticatable, :rememberable,
32
+ :resource_authenticatable
33
+
34
+ self.site = 'https://some.api.com'
35
+ end
36
+
37
+ ```
38
+
39
+ ## Installation
40
+ Add this line to your application's Gemfile:
41
+
42
+ ```ruby
43
+ gem 'devise-activeresource'
44
+ ```
45
+
46
+ And then execute:
47
+ ```bash
48
+ $ bundle
49
+ ```
50
+
51
+ Or install it yourself as:
52
+ ```bash
53
+ $ gem install devise-activeresource
54
+ ```
55
+
56
+ ## License
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Devise::Activeresource'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ # Rake::TestTask.new(:test) do |t|
22
+ # t.libs << 'test'
23
+ # t.pattern = 'test/**/*_test.rb'
24
+ # t.verbose = false
25
+ # end
26
+
27
+ Rake::TestTask.new(:test) do |test|
28
+ ENV['DEVISE_ORM'] ||= 'active_resource'
29
+ ENV['DEVISE_PATH'] ||= File.join(File.dirname(__FILE__), '../devise')
30
+ unless File.exist?(ENV['DEVISE_PATH'])
31
+ puts "Specify the path to devise (e.g. rake DEVISE_PATH=/path/to/devise). Not found at #{ENV['DEVISE_PATH']}"
32
+ exit
33
+ end
34
+ test.libs << 'lib' << 'test'
35
+ test.libs << "#{ENV['DEVISE_PATH']}/lib"
36
+ test.libs << "#{ENV['DEVISE_PATH']}/test"
37
+ test.test_files = FileList["#{ENV['DEVISE_PATH']}/test/**/*_test.rb"].exclude("#{ENV['DEVISE_PATH']}/test/**/serializable_test.rb")
38
+ test.verbose = false
39
+ end
40
+
41
+ task default: :test
@@ -0,0 +1,14 @@
1
+ require 'devise'
2
+ require 'devise/activeresource/base'
3
+ require 'devise/activeresource/patch_methods'
4
+ require 'devise/models/resource_authenticatable'
5
+
6
+ module Devise
7
+ module Activeresource
8
+ end
9
+ end
10
+
11
+ if defined?(ActiveResource::Base)
12
+ require 'devise/activeresource/adapter'
13
+ Devise.add_module :resource_authenticatable, model: 'devise/models/resource_authenticatable'
14
+ end
@@ -0,0 +1,106 @@
1
+ module Devise
2
+ module Activeresource
3
+ # Adapter based on orm_adapter interface
4
+ # https://github.com/ianwhite/orm_adapter/blob/master/lib/orm_adapter/base.rb
5
+ class Adapter
6
+ attr_reader :klass
7
+
8
+ def initialize(klass)
9
+ @klass = klass
10
+ end
11
+
12
+ # Get a list of column/property/field names
13
+ def column_names
14
+ klass.attributes
15
+ end
16
+
17
+ # Get an instance by id of the model. Raises an error if a model is not found.
18
+ # This should comply with ActiveModel#to_key API, i.e.:
19
+ #
20
+ # User.to_adapter.get!(@user.to_key) == @user
21
+ #
22
+ def get!(id)
23
+ klass.find(wrap_key(id))
24
+ end
25
+
26
+ # Get an instance by id of the model. Returns nil if a model is not found.
27
+ # This should comply with ActiveModel#to_key API, i.e.:
28
+ #
29
+ # User.to_adapter.get(@user.to_key) == @user
30
+ #
31
+ def get(id)
32
+ klass.find(wrap_key(id))
33
+ rescue ActiveResource::ResourceNotFound
34
+ nil
35
+ end
36
+
37
+ # Find the first instance, optionally matching conditions
38
+ #
39
+ # You can call with just conditions, providing a hash
40
+ #
41
+ # User.to_adapter.find_first :name => "Fred", :age => 23
42
+ #
43
+ # User.to_adapter.find_first :conditions => {:name => "Fred", :age => 23}
44
+ #
45
+ def find_first(options = {})
46
+ conditions, limit, offset = extract_conditions!(options)
47
+ params = conditions.merge(limit_offset_hash(limit, offset))
48
+ klass.find(:first, params: params)
49
+ rescue ActiveResource::InvalidRequestError
50
+ nil
51
+ end
52
+
53
+ # Find all models, optionally matching conditions, and specifying order
54
+ # @see OrmAdapter::Base#find_first for how to specify order and conditions
55
+ def find_all(options = {})
56
+ conditions, limit, offset = extract_conditions!(options)
57
+ params = conditions.merge(limit_offset_hash(limit, offset))
58
+ klass.find(:all, params: params)
59
+ end
60
+
61
+ # Create a model using attributes
62
+ def create!(attributes = {})
63
+ klass.create(attributes)
64
+ end
65
+
66
+ # Destroy an instance by passing in the instance itself.
67
+ def destroy(object)
68
+ object.destroy
69
+ end
70
+
71
+ protected
72
+
73
+ def valid_object?(object)
74
+ object.class == klass
75
+ end
76
+
77
+ def wrap_key(key)
78
+ key.is_a?(Array) ? key.first : key
79
+ end
80
+
81
+ # given an options hash,
82
+ # with optional :conditions, :limit and :offset keys,
83
+ # returns conditions, limit and offset
84
+ def extract_conditions!(options = {})
85
+ _order = options.delete(:order)
86
+ limit = options.delete(:limit)
87
+ offset = options.delete(:offset)
88
+ conditions = options.delete(:conditions) || options
89
+
90
+ [conditions, limit, offset]
91
+ end
92
+
93
+ def limit_offset_hash(limit, offset)
94
+ response = {}
95
+ response[:limit] = limit if limit && limit > 1
96
+ response[:offset] = offset if offset && offset > 1
97
+ response
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ ActiveSupport.on_load(:active_resource) do
104
+ prepend ::Devise::Activeresource::PatchMethods
105
+ include ::Devise::Activeresource::Base
106
+ end
@@ -0,0 +1,24 @@
1
+ module Devise
2
+ module Activeresource
3
+ module Base
4
+ module ClassMethods
5
+ def to_adapter
6
+ @to_adapter ||= Devise::Activeresource::Adapter.new(self)
7
+ end
8
+
9
+ def increment_counter(counter_name, id)
10
+ element = find(id)
11
+ return false if element.nil?
12
+
13
+ value = element.send(counter_name)
14
+ element.send("#{counter_name}=", value + 1)
15
+ element.save
16
+ end
17
+ end
18
+
19
+ def self.included(base)
20
+ base.extend(ClassMethods)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ module Devise
2
+ module Activeresource
3
+ module PatchMethods
4
+ def initialize(*)
5
+ super
6
+ return unless self.class.schema
7
+
8
+ keys = self.class.schema.select { |_k, v| v == 'timestamp' }.keys
9
+ keys.each do |k|
10
+ value = send(k.to_sym)
11
+ next unless value.is_a?(String)
12
+
13
+ send("#{k}=".to_sym, Time.parse(value))
14
+ end
15
+ end
16
+
17
+ def invalid?
18
+ !valid?
19
+ end
20
+
21
+ def assign_attributes(attributes = {})
22
+ unless attributes.respond_to?(:to_hash)
23
+ raise ArgumentError, 'expected attributes to be able to convert'\
24
+ " to Hash, got #{attributes.inspect}"
25
+ end
26
+
27
+ attributes = attributes.to_hash
28
+ attributes.each do |key, value|
29
+ send("#{key}=".to_sym, value)
30
+ end
31
+ end
32
+
33
+ def [](attribute_name)
34
+ if attributes.include?(attribute_name)
35
+ attributes[attribute_name]
36
+ elsif respond_to?(attribute_name)
37
+ send(attribute_name)
38
+ else
39
+ super
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,6 @@
1
+ module Devise
2
+ module Activeresource
3
+ class Railtie < ::Rails::Railtie
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Devise
2
+ module Activeresource
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ module Devise
2
+ module Models
3
+ module ResourceAuthenticatable
4
+ def update_with_password(params)
5
+ current_password = params.delete(:current_password)
6
+
7
+ if params[:password].blank?
8
+ params.delete(:password)
9
+ params.delete(:password_confirmation) if params[:password_confirmation].blank?
10
+ end
11
+
12
+ result = if valid_password?(current_password)
13
+ update_attributes(params)
14
+ else
15
+ assign_attributes(params)
16
+ valid?
17
+ errors.add(:current_password, current_password.blank? ? :blank : :invalid)
18
+ false
19
+ end
20
+
21
+ clean_up_passwords
22
+ result
23
+ end
24
+
25
+ def update_without_password(params)
26
+ params.delete(:password)
27
+ params.delete(:password_confirmation)
28
+
29
+ result = update_attributes(params)
30
+ clean_up_passwords
31
+ result
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ ActiveSupport.on_load(:active_resource) do
2
+ extend Devise::Models
3
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise-activeresource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Francisco Caiceo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeresource
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: devise
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 4.7.1
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 4.7.1
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rails
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 5.0.0
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '7'
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 5.0.0
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '7'
67
+ description: Plugin to use Active Resource in Devise
68
+ email:
69
+ - jfcaiceo55@gmail.com
70
+ executables: []
71
+ extensions: []
72
+ extra_rdoc_files: []
73
+ files:
74
+ - MIT-LICENSE
75
+ - README.md
76
+ - Rakefile
77
+ - lib/devise/activeresource.rb
78
+ - lib/devise/activeresource/adapter.rb
79
+ - lib/devise/activeresource/base.rb
80
+ - lib/devise/activeresource/patch_methods.rb
81
+ - lib/devise/activeresource/railtie.rb
82
+ - lib/devise/activeresource/version.rb
83
+ - lib/devise/models/resource_authenticatable.rb
84
+ - lib/devise/orm/active_resource.rb
85
+ homepage: https://github.com/jfcaiceo/devise-activeresource
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.7.6.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Support for using Active Resource with Devise
109
+ test_files: []