backframe 0.0.9 → 0.0.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1deb27bbff63479a0ba3ecae41f0ab8e61ab5dfd
4
- data.tar.gz: 05d88ad179a7ceff72019d9f154d4d5a64eaf3a2
3
+ metadata.gz: 3de06cb531fbad921e5fafac1a04b01512061675
4
+ data.tar.gz: 362c046128e883f70b9c523e58ec771bc22814d0
5
5
  SHA512:
6
- metadata.gz: a89ad1c1b5ec4633115edb1cc72806543f6d386b442c4ab49e0c85e3e50498de0339db4ad871299f34d5c68744512bf64dacc5b830c3bd971935c5843e6ba522
7
- data.tar.gz: 9f642996c213589a3961909c7f776c0f0cc93bf2fe3ae9b6875f0672002da0e7a9449222e9ff43aa9c07e294975b9084629ddd62086861b75918390a042b36a8
6
+ metadata.gz: 2f22db57f526565517c16f8508db58f2cb647a85f0ea231c4d0796a544027d8d4fcba2f1e32749e87a56a7618b1999636edd58da65adbac83bc8d0ebf5a04bca
7
+ data.tar.gz: 0e4a21a30dd1e73875a29e1c09d3d03f57e7a3bbabe3c98a6b9a0e756722d98c2ed0d8a582907282e56279b94058ea8436e1fc6857738963697cd1490971c24c
data/Gemfile CHANGED
@@ -13,3 +13,4 @@ gem 'rails'
13
13
  gem 'rake'
14
14
  gem 'rspec-rails'
15
15
  gem 'sqlite3'
16
+ gem 'liquid'
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'active_support'
2
4
  require 'active_support/inflector'
3
5
 
@@ -16,7 +18,7 @@ module Backframe
16
18
  before_action :load_#{model.underscore}, :except => :show
17
19
 
18
20
  def show
19
- @activation = Activation.find_by(:token => params[:token])
21
+ @activation = Backframe::Activation.find_by(:token => params[:token])
20
22
  if @activation.nil?
21
23
  flash[:error] = I18n.t(:activation_invalid)
22
24
  redirect_to account_signin_path
@@ -49,7 +51,7 @@ module Backframe
49
51
  private
50
52
 
51
53
  def load_#{model.underscore}
52
- @activation = Activation.find_by(:id => session[:activation_id])
54
+ @activation = Backframe::Activation.find_by(:id => session[:activation_id])
53
55
  @user = @activation.user
54
56
  if @user.nil?
55
57
  flash[:error] = I18n.t(:activation_invalid)
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module ActsAsAPI
3
5
  class Adapter < ActiveModel::Serializer::Adapter::Attributes
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module ActsAsAPI
3
5
  module Errors
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module ActsAsAPI
3
5
  module Headers
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module ActsAsAPI
3
5
  module Page
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'write_xlsx'
2
4
 
3
5
  require 'active_support'
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'active_support'
2
4
  require 'active_support/inflector'
3
5
 
@@ -32,7 +34,7 @@ module Backframe
32
34
  end
33
35
 
34
36
  def show
35
- @reset = Reset.find_by(:token => params[:token])
37
+ @reset = Backframe::Reset.find_by(:token => params[:token])
36
38
  if @reset.nil?
37
39
  flash[:error] = I18n.t(:reset_invalid)
38
40
  redirect_to '#{arguments[:prefix]}/signin'
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module ActsAsResource
3
5
  module Actions
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'active_support'
2
4
 
3
5
  require 'backframe/actioncontroller/acts_as_resource/actions'
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'active_support'
2
4
  require 'active_support/inflector'
3
5
 
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ module Backframe
4
+ module ActsAsActivable
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ def self.included(base)
9
+ base.send :extend, ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ def acts_as_activable(entity, text, link)
15
+
16
+ class_eval <<-EOV
17
+
18
+ def activities
19
+ conditions = []
20
+ conditions << '("activities"."object1_type"=? AND "activities"."object1_id"=?)'
21
+ conditions << '("activities"."object2_type"=? AND "activities"."object2_id"=?)'
22
+ conditions << '"activities"."subject_type"=? && "activities"."subject_id"=?' if self.is_a?(Admin) || self.is_a?(Customer)
23
+ fragment = [conditions.join(' OR '), self.class.name, self.id, self.class.name, self.id]
24
+ fragment.concat([self.class.name, self.id]) if self.is_a?(Admin) || self.is_a?(Customer)
25
+ Activity.where(fragment)
26
+ end
27
+
28
+ def activity_entity
29
+ "#{entity}"
30
+ end
31
+
32
+ def activity_text
33
+ self.#{text}
34
+ end
35
+
36
+ def activity_link
37
+ link = "#{link}"
38
+ "#{link}".scan(/\\\[([\\\w\\\_]*)\\\]/).each do |token|
39
+ link = link.gsub('['+token.first+']', self.send(token.first).to_s)
40
+ end
41
+ link
42
+ end
43
+ EOV
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ module Backframe
4
+ module ActsAsDistinct
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ def self.included(base)
9
+ base.send :extend, ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ def acts_as_distinct(*args)
15
+
16
+ field = args[0]
17
+ arguments = args[1]
18
+
19
+ class_eval <<-EOV
20
+
21
+ after_initialize :set_default_#{field}, :if => Proc.new { |m| m.new_record? && m.#{arguments[:parent]}.present? }
22
+ after_save :toggle_#{field}_distinction, :if => Proc.new { |m| m.#{arguments[:parent]}.present? && m.#{field} && (m.new_record? || m.#{field}_changed?) }
23
+
24
+ scope :#{field.to_s.gsub('is_', '')}, -> { where(:#{field} => true) }
25
+
26
+ def #{field.to_s.gsub('is_', '')}?
27
+ return self.#{field}
28
+ end
29
+
30
+ private
31
+
32
+ def set_default_#{field}
33
+ self.#{field} = self.#{arguments[:parent]}.#{arguments[:association]}.empty?
34
+ end
35
+
36
+ def toggle_#{field}_distinction
37
+ self.#{arguments[:parent]}.#{arguments[:association]}.where('id != ?', self.id).update_all(:#{field} => false)
38
+ end
39
+
40
+ EOV
41
+
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+ end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module ActsAsOrderable
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module ActsAsStatus
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module ActsAsUser
3
5
  extend ActiveSupport::Concern
@@ -12,8 +14,8 @@ module Backframe
12
14
 
13
15
  attr_accessor :password, :change_password, :set_password, :old_password, :new_password, :confirm_password, :confirm_email
14
16
 
15
- has_many :activations, :dependent => :destroy
16
- has_many :resets, :dependent => :destroy
17
+ has_many :activations, :class_name => 'Backframe::Activation', :dependent => :destroy, :as => :user
18
+ has_many :resets, :class_name => 'Backframe::Reset', :dependent => :destroy, :as => :user
17
19
 
18
20
  after_validation :set_new_password, :if => Proc.new { |u| u.new_password.present? }
19
21
  after_create :activate
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module FilterSort
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module Migration
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  module Mime
3
5
  extend self
@@ -0,0 +1,60 @@
1
+ # encoding: utf-8
2
+
3
+ module Backframe
4
+ class Activation < ActiveRecord::Base
5
+
6
+ liquid_methods :code, :url
7
+
8
+ belongs_to :user, :polymorphic => true
9
+
10
+ validates_presence_of :user, :token
11
+
12
+ after_initialize :init, :if => Proc.new { |o| o.new_record? }
13
+ after_create :enforce_uniqueness
14
+ after_create :send_email
15
+
16
+ def claim
17
+ self.claimed_at = Time.zone.now
18
+ self.is_active = false
19
+ self.save
20
+ self.user.update_attributes(:is_active => true, :activated_at => Time.zone.now)
21
+ end
22
+
23
+ def expired?
24
+ !self.is_active
25
+ end
26
+
27
+ def url
28
+ return "#{Rails.application.config.root_url}/admin/activation/#{self.token}" if self.user_type == 'Admin'
29
+ return "#{Rails.application.config.root_url}/account/activation/#{self.token}" if self.user_type == 'Customer'
30
+ end
31
+
32
+ private
33
+
34
+ def init
35
+ self.token ||= SecureRandom.hex(32).to_s.upcase[0,32]
36
+ self.is_active = true
37
+ end
38
+
39
+ def set_active
40
+ self.is_active = true
41
+ end
42
+
43
+ def enforce_uniqueness
44
+ self.user.activations.where('id != ?', self.id).update_all(:is_active => false)
45
+ end
46
+
47
+ def send_email
48
+ return if Rails.env.test?
49
+ if self.user_type == 'Admin'
50
+ ApplicationMailer.activation(self).deliver_now!
51
+ elsif self.user_type == 'Customer'
52
+ template = EmailTemplate.find_by(:code => 'account_activation')
53
+ email = EmailDelivery.new(:customer => self.user, :subject => template.subject, :body => template.body)
54
+ email.personalize(:customer => self.user, :activation => self)
55
+ email.save
56
+ end
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ module Backframe
4
+ class Activity < ActiveRecord::Base
5
+
6
+ belongs_to :subject, :polymorphic => true
7
+ belongs_to :object1, :polymorphic => true
8
+ belongs_to :object2, :polymorphic => true
9
+ belongs_to :story
10
+
11
+ validates_presence_of :story, :subject
12
+
13
+ default_scope -> { includes(:subject,:object1,:object2,:story).order(:created_at => :desc) }
14
+
15
+ def text=(text)
16
+ self.story = Story.find_or_initialize_by(:text => text)
17
+ end
18
+
19
+ def subject=(object)
20
+ self.subject_type = object.class.name
21
+ self.subject_text = object.activity_text
22
+ self.subject_id = object.id
23
+ end
24
+
25
+ def object1=(object)
26
+ self.object1_type = object.class.name
27
+ self.object1_text = object.activity_text
28
+ self.object1_id = object.id
29
+ object
30
+ end
31
+
32
+ def object2=(object)
33
+ self.object2_type = object.class.name
34
+ self.object2_text = object.activity_text
35
+ self.object2_id = object.id
36
+ object
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ module Backframe
4
+ class Reset < ActiveRecord::Base
5
+
6
+ liquid_methods :code, :url
7
+
8
+ belongs_to :user, :polymorphic => true
9
+
10
+ validates_presence_of :user, :token
11
+
12
+ after_initialize :init, :if => Proc.new { |o| o.new_record? }
13
+ after_create :enforce_uniqueness
14
+ after_create :send_email
15
+
16
+ def claim
17
+ self.claimed_at = Time.zone.now
18
+ self.is_active = false
19
+ self.save
20
+ self.user.update_attributes(:is_active => true, :activated_at => Time.zone.now)
21
+ end
22
+
23
+ def expired?
24
+ !self.is_active
25
+ end
26
+
27
+ def url
28
+ return "#{Rails.application.config.root_url}/admin/reset/#{self.token}" if self.user_type == 'Admin'
29
+ return "#{Rails.application.config.root_url}/account/reset/#{self.token}" if self.user_type == 'Customer'
30
+ end
31
+
32
+ private
33
+
34
+ def init
35
+ self.token ||= SecureRandom.hex(32).to_s.upcase[0,32]
36
+ self.is_active = true
37
+ end
38
+
39
+ def set_active
40
+ self.is_active = true
41
+ end
42
+
43
+ def enforce_uniqueness
44
+ self.user.resets.where('id != ?', self.id).update_all(:is_active => false)
45
+ end
46
+
47
+ def send_email
48
+ if self.user_type == 'Admin'
49
+ ApplicationMailer.reset(self).deliver_now!
50
+ elsif self.user_type == 'Customer'
51
+ template = EmailTemplate.find_by(:code => 'password_reset')
52
+ email = EmailDelivery.new(:customer => self.user, :subject => template.subject, :body => template.body)
53
+ email.personalize(:customer => self.user, :reset => self)
54
+ email.save
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
4
  class Railtie < ::Rails::Railtie
3
5
  initializer 'backframe' do |_app|
@@ -6,6 +8,8 @@ module Backframe
6
8
  ActionController::Base.send(:include, Backframe::ActsAsActivation)
7
9
  ActionController::Base.send(:include, Backframe::ActsAsReset)
8
10
  ActionController::Base.send(:include, Backframe::ActsAsSession)
11
+ ActiveRecord::Base.send(:include, Backframe::ActsAsActivable)
12
+ ActiveRecord::Base.send(:include, Backframe::ActsAsDistinct)
9
13
  ActiveRecord::Base.send(:include, Backframe::ActsAsOrderable)
10
14
  ActiveRecord::Base.send(:include, Backframe::ActsAsStatus)
11
15
  ActiveRecord::Base.send(:include, Backframe::ActsAsUser)
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ module Backframe
4
+ class ActivitySerializer < ActiveModel::Serializer
5
+
6
+ cache :key => :object
7
+ attributes :id, :subject, :story, :object1, :object2, :created_at, :updated_at
8
+
9
+ def subject
10
+ if object.subject.present?
11
+ { :link => object.subject.activity_link, :photo => (object.subject.photo.present?) ? object.subject.photo.url(:small) : nil, :text => object.subject.full_name }
12
+ elsif object.subject_text.present?
13
+ { :text => object.subject_text }
14
+ else
15
+ nil
16
+ end
17
+ end
18
+
19
+ def story
20
+ object.story.text
21
+ end
22
+
23
+ def object1
24
+ if object.object1.present?
25
+ { :link => object.object1.activity_link, :entity => object.object1.activity_entity, :text => object.object1.activity_text }
26
+ elsif object.object1_text.present?
27
+ { :text => object.object1_text }
28
+ else
29
+ nil
30
+ end
31
+ end
32
+
33
+ def object2
34
+ if object.object2.present?
35
+ { :link => object.object2.activity_link, :entity => object.object2.activity_entity, :text => object.object2.activity_text }
36
+ elsif object.object2_text.present?
37
+ { :text => object.object2_text }
38
+ else
39
+ nil
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Backframe
2
- VERSION = '0.0.9'
4
+ VERSION = '0.0.10'
3
5
  end
data/lib/backframe.rb CHANGED
@@ -1,22 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_record'
4
+ require 'active_record/version'
5
+ require 'active_support/core_ext/module'
6
+ require 'liquid'
7
+
1
8
  require 'backframe/actioncontroller/acts_as_activation'
2
9
  require 'backframe/actioncontroller/acts_as_api'
3
10
  require 'backframe/actioncontroller/acts_as_reset'
4
11
  require 'backframe/actioncontroller/acts_as_resource'
5
12
  require 'backframe/actioncontroller/acts_as_session'
13
+ require 'backframe/activerecord/acts_as_activable'
14
+ require 'backframe/activerecord/acts_as_distinct'
6
15
  require 'backframe/activerecord/acts_as_orderable'
7
16
  require 'backframe/activerecord/acts_as_status'
8
17
  require 'backframe/activerecord/acts_as_user'
9
18
  require 'backframe/activerecord/filter_sort'
10
19
  require 'backframe/activerecord/migration'
20
+ require 'backframe/models/activity'
21
+ require 'backframe/models/activation'
22
+ require 'backframe/models/reset'
23
+ require 'backframe/serializers/activity_serializer'
11
24
 
12
25
  module Backframe
26
+
27
+ extend ActiveSupport::Autoload
28
+
29
+ autoload :Activity
30
+ autoload :Activation
31
+ autoload :Reset
32
+
13
33
  module Exceptions
14
34
  class Unauthenticated < StandardError; end
15
35
  class Unauthorized < StandardError; end
16
36
  end
17
- end
18
37
 
38
+ end
19
39
  if defined? Rails
20
40
  require 'backframe/mime'
21
41
  require 'backframe/railtie'
22
- end
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backframe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Kops
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-09 00:00:00.000000000 Z
12
+ date: 2016-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: write_xlsx
@@ -105,13 +105,19 @@ files:
105
105
  - lib/backframe/actioncontroller/acts_as_resource.rb
106
106
  - lib/backframe/actioncontroller/acts_as_resource/actions.rb
107
107
  - lib/backframe/actioncontroller/acts_as_session.rb
108
+ - lib/backframe/activerecord/acts_as_activable.rb
109
+ - lib/backframe/activerecord/acts_as_distinct.rb
108
110
  - lib/backframe/activerecord/acts_as_orderable.rb
109
111
  - lib/backframe/activerecord/acts_as_status.rb
110
112
  - lib/backframe/activerecord/acts_as_user.rb
111
113
  - lib/backframe/activerecord/filter_sort.rb
112
114
  - lib/backframe/activerecord/migration.rb
113
115
  - lib/backframe/mime.rb
116
+ - lib/backframe/models/activation.rb
117
+ - lib/backframe/models/activity.rb
118
+ - lib/backframe/models/reset.rb
114
119
  - lib/backframe/railtie.rb
120
+ - lib/backframe/serializers/activity_serializer.rb
115
121
  - lib/backframe/version.rb
116
122
  - spec/backframe/acts_as_api_spec.rb
117
123
  - spec/backframe/acts_as_resource_spec.rb