kirgudu_common 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/javascripts/kirgudu_common/application.js +13 -0
  6. data/app/assets/stylesheets/kirgudu_common/application.css +15 -0
  7. data/app/helpers/kirgudu_common/application_helper.rb +4 -0
  8. data/app/models/kirgudu_common/concerns/models/administrated_by.rb +27 -0
  9. data/app/models/kirgudu_common/concerns/models/created_at.rb +36 -0
  10. data/app/models/kirgudu_common/concerns/models/created_by.rb +25 -0
  11. data/app/models/kirgudu_common/concerns/models/deletion_status_id.rb +28 -0
  12. data/app/models/kirgudu_common/concerns/models/id.rb +24 -0
  13. data/app/models/kirgudu_common/concerns/models/is_active.rb +24 -0
  14. data/app/models/kirgudu_common/concerns/models/managed_by.rb +25 -0
  15. data/app/models/kirgudu_common/concerns/models/name.rb +36 -0
  16. data/app/models/kirgudu_common/concerns/models/order_by.rb +21 -0
  17. data/app/models/kirgudu_common/concerns/models/owned_by.rb +26 -0
  18. data/app/models/kirgudu_common/concerns/models/person_age.rb +49 -0
  19. data/app/models/kirgudu_common/concerns/models/person_full_name.rb +41 -0
  20. data/app/models/kirgudu_common/concerns/models/portal_id.rb +26 -0
  21. data/app/models/kirgudu_common/concerns/models/portal_user_id.rb +26 -0
  22. data/app/models/kirgudu_common/concerns/models/slug.rb +43 -0
  23. data/app/models/kirgudu_common/concerns/models/status_id.rb +28 -0
  24. data/app/models/kirgudu_common/concerns/models/title.rb +40 -0
  25. data/app/models/kirgudu_common/concerns/models/updated_at.rb +41 -0
  26. data/app/models/kirgudu_common/concerns/models/updated_by.rb +29 -0
  27. data/app/models/kirgudu_common/concerns/models/user_id.rb +35 -0
  28. data/app/models/kirgudu_common/portal.rb +78 -0
  29. data/app/models/kirgudu_common/portal_user.rb +54 -0
  30. data/app/models/kirgudu_common/user.rb +104 -0
  31. data/app/models/kirgudu_common/view_template.rb +6 -0
  32. data/app/models/kirgudu_common/view_template_element.rb +6 -0
  33. data/app/views/layouts/kirgudu_common/application.html.erb +14 -0
  34. data/config/routes.rb +2 -0
  35. data/db/migrate/20130913084129_create_kirgudu_common_users.rb +55 -0
  36. data/db/migrate/20131114161828_create_kirgudu_common_view_templates.rb +30 -0
  37. data/db/migrate/20131114165013_create_kirgudu_common_view_template_elements.rb +34 -0
  38. data/db/migrate/20140208133148_create_kirgudu_common_portals.rb +37 -0
  39. data/db/migrate/20140208134642_create_kirgudu_common_portal_users.rb +23 -0
  40. data/lib/kirgudu_common/engine.rb +5 -0
  41. data/lib/kirgudu_common.rb +4 -0
  42. data/lib/tasks/kirgudu_common_tasks.rake +4 -0
  43. data/test/dummy/README.rdoc +28 -0
  44. data/test/dummy/Rakefile +6 -0
  45. data/test/dummy/app/assets/javascripts/application.js +13 -0
  46. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  47. data/test/dummy/app/controllers/application_controller.rb +5 -0
  48. data/test/dummy/app/helpers/application_helper.rb +2 -0
  49. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  50. data/test/dummy/bin/bundle +3 -0
  51. data/test/dummy/bin/rails +4 -0
  52. data/test/dummy/bin/rake +4 -0
  53. data/test/dummy/config/application.rb +23 -0
  54. data/test/dummy/config/boot.rb +5 -0
  55. data/test/dummy/config/database.yml +25 -0
  56. data/test/dummy/config/environment.rb +5 -0
  57. data/test/dummy/config/environments/development.rb +37 -0
  58. data/test/dummy/config/environments/production.rb +82 -0
  59. data/test/dummy/config/environments/test.rb +39 -0
  60. data/test/dummy/config/initializers/assets.rb +8 -0
  61. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  62. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  63. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  64. data/test/dummy/config/initializers/inflections.rb +16 -0
  65. data/test/dummy/config/initializers/mime_types.rb +4 -0
  66. data/test/dummy/config/initializers/session_store.rb +3 -0
  67. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  68. data/test/dummy/config/locales/en.yml +23 -0
  69. data/test/dummy/config/routes.rb +4 -0
  70. data/test/dummy/config/secrets.yml +22 -0
  71. data/test/dummy/config.ru +4 -0
  72. data/test/dummy/public/404.html +67 -0
  73. data/test/dummy/public/422.html +67 -0
  74. data/test/dummy/public/500.html +66 -0
  75. data/test/dummy/public/favicon.ico +0 -0
  76. data/test/integration/navigation_test.rb +10 -0
  77. data/test/kirgudu_common_test.rb +7 -0
  78. data/test/test_helper.rb +15 -0
  79. metadata +199 -0
@@ -0,0 +1,41 @@
1
+ module KirguduCommon
2
+ module Concerns::Models::PersonFullName
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+
9
+ def full_name
10
+ fn = if self.respond_to?(:first_name)
11
+ self.first_name.to_s
12
+ elsif self.respond_to?(:firstname)
13
+ self.firstname
14
+ else
15
+ ""
16
+ end rescue ""
17
+
18
+ ln = if self.respond_to?(:last_name)
19
+ self.last_name.to_s
20
+ elsif self.respond_to?(:lastname)
21
+ self.lastname.to_s
22
+ else
23
+ ""
24
+ end rescue ""
25
+
26
+ mn = if self.respond_to?(:middle_name)
27
+ self.middle_name.to_s
28
+ elsif self.respond_to?(:middlename)
29
+ self.middlename.to_s
30
+ else
31
+ ""
32
+ end rescue ""
33
+
34
+ "#{ln} #{fn} #{mn}".strip rescue ""
35
+ end
36
+
37
+
38
+ end
39
+ end
40
+
41
+
@@ -0,0 +1,26 @@
1
+ module KirguduCommon
2
+ module Concerns::Models::PortalId
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ before_query :query_add_portal_id
7
+
8
+ belongs_to :portal, class_name: "::KirguduCommon::Portal", foreign_key: :portal_id
9
+ scope :by_portal_id, lambda { |value| where(portal_id: value) if value }
10
+ end
11
+
12
+
13
+ def portal_name
14
+ self.portal.name if self.portal
15
+ end
16
+
17
+ module ClassMethods
18
+ def query_add_portal_id(query, filters)
19
+ query.by_portal_id(filters[:portal_id])
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+
@@ -0,0 +1,26 @@
1
+ module KirguduCommon
2
+ module Concerns::Models::PortalUserId
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ before_query :query_add_portal_user_id
7
+
8
+ belongs_to :portal_user, class_name: "::KirguduCommon::PortalUser", foreign_key: :portal_user_id
9
+ scope :by_portal_user, lambda { |value| where(portal_user_id: value) if value }
10
+ end
11
+
12
+
13
+ def portal_user_name
14
+ self.portal_user.full_name if self.portal_user
15
+ end
16
+
17
+ module ClassMethods
18
+ def query_add_portal_user_id(query, filters)
19
+ query.by_portal_user_id(filters[:portal_user_id])
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+
@@ -0,0 +1,43 @@
1
+ module KirguduCommon
2
+ module Concerns::Models::Slug
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ ############################ FILTERS ###############################
7
+ before_query :query_add_slug
8
+ before_validation :normalize_slug
9
+
10
+ ########################## VALIDATIONS ###############################
11
+
12
+ validates_presence_of :slug
13
+ validates_length_of :slug, maximum: 128, allow_blank: true
14
+ validates_slug_format_of :slug, allow_blank: true
15
+
16
+ ########################## SCOPES ###############################
17
+
18
+ scope :with_slug, lambda {
19
+ |value|
20
+ like_value = "%#{value.gsub('%', ' ').strip}%" rescue nil
21
+ where { (slug.like like_value) } if like_value
22
+ }
23
+ end
24
+
25
+
26
+ ########################## INSTANCE METHODS ###############################
27
+
28
+ def normalize_slug
29
+ self.slug = self.slug.downcase.strip if self.slug
30
+ end
31
+
32
+ ########################## CLASS METHODS ###############################
33
+
34
+ module ClassMethods
35
+ def query_add_slug(query, filters)
36
+ query.with_slug(filters[:slug])
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+
@@ -0,0 +1,28 @@
1
+ module KirguduCommon
2
+ module Concerns::Models::StatusId
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ ############################ FILTERS ###############################
7
+ before_query :query_add_status_id
8
+
9
+ ############################ SCOPES ###############################
10
+ base.scope :by_status, lambda { |value| where(status_id: value) if value }
11
+ end
12
+
13
+ ############################ INSTANCE METHODS ###############################
14
+
15
+ def status_name
16
+ I18n.t("kirgudu_base.common.enums.deletion_status.#{self.status_id}")
17
+ end
18
+
19
+ ############################ CLASS METHODS ###############################
20
+ module ClassMethods
21
+ def query_add_status_id(query, filters)
22
+ query.by_status_id(filters[:status_id])
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+
@@ -0,0 +1,40 @@
1
+ module KirguduCommon
2
+ module Concerns::Models::Title
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ ############################ FILTERS ###############################
7
+ before_validation :normalize_title
8
+ before_query :query_add_title
9
+
10
+ ############################ VALIDATIONS ###############################
11
+ validates_length_of :title, maximum: 128, allow_blank: true
12
+
13
+ ############################ SCOPES ###############################
14
+
15
+ scope :with_title, lambda {
16
+ |value|
17
+ like_value = "%#{value.gsub('%', ' ').strip}%" rescue nil
18
+ where { (title.like like_value) } if like_value
19
+ }
20
+ scope :by_title, lambda { |value| where(title: value) if value }
21
+ end
22
+
23
+ ############################ INSTANCE METHODS ###############################
24
+ def normalize_title
25
+ self.title.strip! if self.title
26
+ end
27
+
28
+
29
+ ############################ CLASS METHODS ###############################
30
+ module ClassMethods
31
+ def query_add_title(query, filters)
32
+ query.by_title(filters[:title])
33
+ end
34
+ end
35
+
36
+
37
+ end
38
+ end
39
+
40
+
@@ -0,0 +1,41 @@
1
+ module KirguduCommon
2
+ module Concerns::Models::UpdatedAt
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+
7
+ before_query :query_add_updated_at
8
+
9
+ base.scope :with_updated_at_me, lambda { |value| base.where { updated_at >= value } if value }
10
+ base.scope :with_updated_at_le, lambda { |value| base.where { updated_at <= value } if value }
11
+
12
+ base.scope :with_updated_at_m, lambda { |value| base.where { updated_at > value } if value }
13
+ base.scope :with_updated_at_l, lambda { |value| base.where { updated_at < value } if value }
14
+ end
15
+
16
+ def updated_at_formatted
17
+ #time_ago_in_words()
18
+ time_distance_in_words = distance_of_time_in_words(self.updated_at, DateTime.now.utc, true)
19
+ time_dt_format = self.updated_at.strftime("%d.%m.%Y %H:%M")
20
+
21
+
22
+ #"#{time_distance_in_words} (#{time_dt_format})"
23
+ end
24
+
25
+ ############################ CLASS METHODS ###############################
26
+ module ClassMethods
27
+
28
+
29
+ def query_add_updated_at(query, filters)
30
+ query
31
+ .with_updated_at_me(filters[:updated_at_me])
32
+ .with_updated_at_le(filters[:updated_at_le])
33
+ .with_updated_at_me(filters[:updated_at_me])
34
+ .with_updated_at_le(filters[:updated_at_le])
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+
41
+
@@ -0,0 +1,29 @@
1
+ module KirguduCommon
2
+ module Concerns::Models::UpdatedBy
3
+ extend ActiveSupport::Concern
4
+
5
+
6
+ included do
7
+ before_query :query_add_updated_by
8
+
9
+ belongs_to :updater, class_name: "::KirguduCommon::User", foreign_key: :updated_by
10
+ scope :by_updated_by, lambda { |value| where(updated_by: value) if value }
11
+
12
+
13
+ end
14
+
15
+
16
+ def updater_name
17
+ updater.full_name if updater
18
+ end
19
+ ############################ CLASS METHODS ###############################
20
+
21
+ module ClassMethods
22
+ def query_add_updated_by(query, filters)
23
+ query.by_updated_by(filters[:updated_by])
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+
@@ -0,0 +1,35 @@
1
+ module KirguduCommon
2
+ module Concerns::Models::UserId
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ before_query :query_add_user_id
7
+
8
+ belongs_to :user, class_name: "::KirguduCommon::User", foreign_key: :user_id
9
+ scope :by_user_id, lambda { |value| where(user_id: value) if value }
10
+ scope :with_user_keyword, lambda { |value|
11
+ like_value = "%#{value.gsub('%', ' ').strip}%" rescue nil
12
+ where { (user.last_name.like like_value) | (user.first_name.like like_value) } if like_value
13
+
14
+ }
15
+ end
16
+
17
+
18
+ def user_name
19
+ self.user.full_name if self.user
20
+ end
21
+
22
+ ############################ CLASS METHODS ###############################
23
+ module ClassMethods
24
+ def query_add_user_id(query, filters)
25
+ query
26
+ .by_user_id(filters[:user_id])
27
+ .with_user_keyword(filters[:user_keyword])
28
+
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+
@@ -0,0 +1,78 @@
1
+ module KirguduCommon
2
+ class Portal < ::KirguduBase::BaseModel
3
+
4
+ include ::KirguduCommon::Concerns::Models::CreatedBy
5
+ include ::KirguduCommon::Concerns::Models::UpdatedBy
6
+ include ::KirguduCommon::Concerns::Models::OwnedBy
7
+ include ::KirguduCommon::Concerns::Models::AdministratedBy
8
+
9
+
10
+ validates_presence_of :dns_name, :name
11
+ validates_uniqueness_of :dns_name
12
+ validates_length_of :dns_name, :name, allow_blank: true, maximum: 180
13
+
14
+
15
+ has_many :portal_users, class_name: ::KirguduCommon::PortalUser, foreign_key: :portal_id, dependent: :destroy
16
+ has_many :users, class_name: ::KirguduCommon::User, through: :portal_users
17
+
18
+
19
+ scope :with_dns_name, lambda { |value|
20
+ if value
21
+ value = value.gsub('%', '')
22
+ like_value = "%#{value}%"
23
+ where { dns_name.like like_value }
24
+ end
25
+ }
26
+
27
+ scope :with_keyword, lambda { |value|
28
+ if value
29
+ value = value.gsub('%', '')
30
+ like_value = "%#{value}%"
31
+ where { (name.like like_value) | (dns_name.like like_value) }
32
+ end
33
+ }
34
+
35
+
36
+ def self.apply_filters_to_query(query, filters={})
37
+ filters ||= {}
38
+
39
+ query = query.with_dns_name(filters[:dns_name])
40
+
41
+ super(query, filters)
42
+ end
43
+
44
+
45
+ def self.generate(name, dns_name, user)
46
+ portal = nil
47
+ time_now = Time.now
48
+ ActiveRecord::Base.transaction do
49
+ portal = ::KirguduCommon::Portal.new(
50
+ uin: ::SecureRandom.uuid,
51
+ created_by: user.id,
52
+ updated_by: user.id,
53
+ owned_by: user.id,
54
+ owned_at: time_now,
55
+ administrated_by: user.id,
56
+ administrator_at: time_now,
57
+ name: name,
58
+ dns_name: dns_name
59
+ )
60
+ if portal.save
61
+ portal_user = ::KirguduCommon::PortalUser.new(
62
+ uin: ::SecureRandom.uuid,
63
+ created_by: user.id,
64
+ updated_by: user.id,
65
+ portal_id: portal.id,
66
+ user_id: user.id
67
+ )
68
+ unless portal_user.save
69
+ raise ActiveRecord::Rollback
70
+ end
71
+ end
72
+ end
73
+ portal
74
+ end
75
+
76
+
77
+ end
78
+ end
@@ -0,0 +1,54 @@
1
+ module KirguduCommon
2
+ class PortalUser < ::KirguduBase::BaseModel
3
+
4
+ include ::KirguduCommon::Concerns::Models::UserId
5
+ include ::KirguduCommon::Concerns::Models::PortalId
6
+ include ::KirguduCommon::Concerns::Models::StatusId
7
+
8
+
9
+ validates_presence_of :user_id, :portal_id
10
+ validates_uniqueness_of :user_id, scope: :portal_id
11
+
12
+
13
+ scope :with_keyword, lambda { |value|
14
+ if value
15
+ value = value.gsub('%', '')
16
+ like_value = "%#{value}%"
17
+ where { (user.first_name.like like_value) | (user.last_name.like like_value) }
18
+ end
19
+ }
20
+
21
+ def self.apply_filters_to_query(query, filters={})
22
+ filters ||= {}
23
+
24
+ filters.merge!({
25
+ order:
26
+ ["#{::KirguduCommon::Portal.table_name}.name asc",
27
+ "#{::KirguduCommon::User.table_name}.last_name asc",
28
+ "#{::KirguduCommon::User.table_name}.first_name asc"].join(", ")
29
+ })
30
+
31
+ query = query.joins(:user).includes(:portal, :user)
32
+
33
+ super(query, filters)
34
+ end
35
+
36
+
37
+ def name_for_breadcrumbs
38
+ self.user_name
39
+ end
40
+
41
+ def to_s
42
+ self.user_name
43
+ end
44
+
45
+ def as_json(options = {})
46
+ # just in case someone says as_json(nil) and bypasses
47
+ # our default...
48
+ super((options || {}).merge({
49
+ :methods => [:user_name, :portal_name]
50
+ }))
51
+ end
52
+
53
+ end
54
+ end