loyal_core 0.0.1

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.
Files changed (139) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +3 -0
  3. data/Rakefile +27 -0
  4. data/app/assets/images/loyal_core/down_rating.gif +0 -0
  5. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/ico_missing.gif +0 -0
  6. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/medium_missing.gif +0 -0
  7. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/mini_missing.gif +0 -0
  8. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/missing.gif +0 -0
  9. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/small_missing.gif +0 -0
  10. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/thumb_missing.gif +0 -0
  11. data/app/assets/images/loyal_core/fallbacks/loyal_core/skin/recipe/avatar/tiny_missing.gif +0 -0
  12. data/app/assets/images/loyal_core/fallbacks/user/avatar/ico_missing.gif +0 -0
  13. data/app/assets/images/loyal_core/fallbacks/user/avatar/medium_missing.gif +0 -0
  14. data/app/assets/images/loyal_core/fallbacks/user/avatar/mini_missing.gif +0 -0
  15. data/app/assets/images/loyal_core/fallbacks/user/avatar/missing.gif +0 -0
  16. data/app/assets/images/loyal_core/fallbacks/user/avatar/small_missing.gif +0 -0
  17. data/app/assets/images/loyal_core/fallbacks/user/avatar/thumb_missing.gif +0 -0
  18. data/app/assets/images/loyal_core/fallbacks/user/avatar/tiny_missing.gif +0 -0
  19. data/app/assets/images/loyal_core/liked.gif +0 -0
  20. data/app/assets/images/loyal_core/unlike.gif +0 -0
  21. data/app/assets/images/loyal_core/up_rating.gif +0 -0
  22. data/app/assets/javascripts/jquery.cookie.js +95 -0
  23. data/app/assets/javascripts/loyal_core/admin/skin/folders.js +2 -0
  24. data/app/assets/javascripts/loyal_core/admin/skin/recipes.js +2 -0
  25. data/app/assets/javascripts/loyal_core/ajax/captchas.js +2 -0
  26. data/app/assets/javascripts/loyal_core/ajax/like_tracks.js +2 -0
  27. data/app/assets/javascripts/loyal_core/ajax/rating_tracks.js +3 -0
  28. data/app/assets/javascripts/loyal_core/ajax/users/sessions.js +2 -0
  29. data/app/assets/javascripts/loyal_core/ajax.js +124 -0
  30. data/app/assets/javascripts/loyal_core/application.js +6 -0
  31. data/app/assets/javascripts/loyal_core/jumps.js +2 -0
  32. data/app/assets/javascripts/loyal_core/tools/append_params_return_to.js +21 -0
  33. data/app/assets/javascripts/loyal_core/tools/editor_content_mode_selector.js +8 -0
  34. data/app/assets/javascripts/loyal_core/tools/font_selector.js +83 -0
  35. data/app/assets/stylesheets/loyal_core/admin/skin/folders.css +4 -0
  36. data/app/assets/stylesheets/loyal_core/admin/skin/recipes.css +4 -0
  37. data/app/assets/stylesheets/loyal_core/ajax/captchas.css +4 -0
  38. data/app/assets/stylesheets/loyal_core/ajax/like_tracks.css +4 -0
  39. data/app/assets/stylesheets/loyal_core/ajax/rating_tracks.css +4 -0
  40. data/app/assets/stylesheets/loyal_core/ajax/users/sessions.css +4 -0
  41. data/app/assets/stylesheets/loyal_core/application.css.scss +18 -0
  42. data/app/assets/stylesheets/loyal_core/jumps.css +4 -0
  43. data/app/controllers/loyal_core/admin/skin/folders_controller.rb +54 -0
  44. data/app/controllers/loyal_core/admin/skin/recipes_controller.rb +52 -0
  45. data/app/controllers/loyal_core/ajax/captchas_controller.rb +26 -0
  46. data/app/controllers/loyal_core/ajax/like_tracks_controller.rb +33 -0
  47. data/app/controllers/loyal_core/ajax/rating_tracks_controller.rb +41 -0
  48. data/app/controllers/loyal_core/ajax/users/sessions_controller.rb +8 -0
  49. data/app/controllers/loyal_core/ajax_controller.rb +14 -0
  50. data/app/controllers/loyal_core/application_controller.rb +6 -0
  51. data/app/controllers/loyal_core/jumps_controller.rb +10 -0
  52. data/app/helpers/loyal_core/admin/skin/folders_helper.rb +5 -0
  53. data/app/helpers/loyal_core/admin/skin/recipes_helper.rb +5 -0
  54. data/app/helpers/loyal_core/ajax/captchas_helper.rb +6 -0
  55. data/app/helpers/loyal_core/ajax/like_tracks_helper.rb +6 -0
  56. data/app/helpers/loyal_core/ajax/rating_tracks_helper.rb +6 -0
  57. data/app/helpers/loyal_core/ajax/users/sessions_helper.rb +5 -0
  58. data/app/helpers/loyal_core/application_helper.rb +6 -0
  59. data/app/helpers/loyal_core/jumps_helper.rb +5 -0
  60. data/app/models/kindeditor/asset.rb +14 -0
  61. data/app/models/kindeditor/file.rb +4 -0
  62. data/app/models/kindeditor/flash.rb +4 -0
  63. data/app/models/kindeditor/image.rb +4 -0
  64. data/app/models/kindeditor/media.rb +4 -0
  65. data/app/models/loyal_core/like_track.rb +15 -0
  66. data/app/models/loyal_core/rating_track.rb +32 -0
  67. data/app/models/loyal_core/skin/dish.rb +10 -0
  68. data/app/models/loyal_core/skin/folder.rb +26 -0
  69. data/app/models/loyal_core/skin/recipe.rb +31 -0
  70. data/app/models/loyal_core/skin.rb +8 -0
  71. data/app/uploaders/loyal_core/asset_uploader.rb +128 -0
  72. data/app/uploaders/loyal_core/avatar_uploader.rb +37 -0
  73. data/app/views/layouts/loyal_core/application.html.erb +14 -0
  74. data/app/views/loyal_core/admin/skin/folders/_form.html.erb +9 -0
  75. data/app/views/loyal_core/admin/skin/folders/edit.html.erb +11 -0
  76. data/app/views/loyal_core/admin/skin/folders/index.html.erb +25 -0
  77. data/app/views/loyal_core/admin/skin/folders/new.html.erb +11 -0
  78. data/app/views/loyal_core/admin/skin/folders/show.html.erb +32 -0
  79. data/app/views/loyal_core/admin/skin/recipes/_form.html.erb +25 -0
  80. data/app/views/loyal_core/admin/skin/recipes/edit.html.erb +11 -0
  81. data/app/views/loyal_core/admin/skin/recipes/index.html.erb +37 -0
  82. data/app/views/loyal_core/admin/skin/recipes/new.html.erb +11 -0
  83. data/app/views/loyal_core/admin/skin/recipes/show.html.erb +16 -0
  84. data/app/views/loyal_core/ajax/captchas/_new.html.erb +1 -0
  85. data/app/views/loyal_core/ajax/captchas/new.html.erb +1 -0
  86. data/app/views/loyal_core/jumps/show.html.erb +12 -0
  87. data/app/views/simple_captcha/_simple_captcha.erb +16 -0
  88. data/config/locales/en/default.en.yml +139 -0
  89. data/config/locales/en/loyal_simple_captcha.en.yml +8 -0
  90. data/config/locales/zh-CN/action_view.zh-CN.yml +8 -0
  91. data/config/locales/zh-CN/default.zh-CN.yml +199 -0
  92. data/config/locales/zh-CN/loyal_simple_captcha.zh-CN.yml +8 -0
  93. data/config/locales/zh-CN/paginate.zh-CN.yml +8 -0
  94. data/config/routes.rb +24 -0
  95. data/db/migrate/20130331104216_create_loyal_core_rating_tracks.rb +19 -0
  96. data/db/migrate/20130331104822_create_loyal_core_like_tracks.rb +19 -0
  97. data/db/migrate/20130413061851_create_kindeditor_assets.rb +17 -0
  98. data/db/migrate/20130610091558_create_loyal_core_skin_folders.rb +48 -0
  99. data/db/migrate/20130610095635_create_loyal_core_skin_recipes.rb +28 -0
  100. data/db/migrate/20130610095938_create_loyal_core_skin_dishes.rb +13 -0
  101. data/lib/loyal_core/action_controller/request_init.rb +33 -0
  102. data/lib/loyal_core/action_controller/seo_methods.rb +47 -0
  103. data/lib/loyal_core/action_controller.rb +4 -0
  104. data/lib/loyal_core/action_helper/ajax/like_tracks_helper.rb +26 -0
  105. data/lib/loyal_core/action_helper/ajax/rating_tracks_helper.rb +35 -0
  106. data/lib/loyal_core/action_helper/font_selector_helper.rb +25 -0
  107. data/lib/loyal_core/action_helper.rb +27 -0
  108. data/lib/loyal_core/action_view/labeled_builder.rb +127 -0
  109. data/lib/loyal_core/action_view.rb +7 -0
  110. data/lib/loyal_core/active_model/fix_counter_cache.rb +39 -0
  111. data/lib/loyal_core/active_model/human_display_able.rb +26 -0
  112. data/lib/loyal_core/active_model/strip_whitespace.rb +27 -0
  113. data/lib/loyal_core/active_model.rb +16 -0
  114. data/lib/loyal_core/acts/bit_able.rb +139 -0
  115. data/lib/loyal_core/acts/content_depart_able.rb +102 -0
  116. data/lib/loyal_core/acts/content_mode_able.rb +88 -0
  117. data/lib/loyal_core/acts/has_avatar.rb +31 -0
  118. data/lib/loyal_core/acts/has_permalink.rb +43 -0
  119. data/lib/loyal_core/acts/like_track_able.rb +90 -0
  120. data/lib/loyal_core/acts/named_filter_able.rb +40 -0
  121. data/lib/loyal_core/acts/rating_track_able.rb +76 -0
  122. data/lib/loyal_core/acts/skin_dish_able.rb +56 -0
  123. data/lib/loyal_core/acts/tree.rb +104 -0
  124. data/lib/loyal_core/acts/uuid_ful.rb +39 -0
  125. data/lib/loyal_core/acts/views_count_able.rb +33 -0
  126. data/lib/loyal_core/acts.rb +35 -0
  127. data/lib/loyal_core/config.rb +116 -0
  128. data/lib/loyal_core/core_ext/singleton_class.rb +9 -0
  129. data/lib/loyal_core/engine.rb +11 -0
  130. data/lib/loyal_core/memoist.rb +204 -0
  131. data/lib/loyal_core/utils/array_util.rb +16 -0
  132. data/lib/loyal_core/utils/config_util.rb +186 -0
  133. data/lib/loyal_core/utils/data_util.rb +181 -0
  134. data/lib/loyal_core/utils/text_util.rb +49 -0
  135. data/lib/loyal_core/utils.rb +8 -0
  136. data/lib/loyal_core/version.rb +4 -0
  137. data/lib/loyal_core.rb +17 -0
  138. data/lib/tasks/loyal_core_tasks.rake +11 -0
  139. metadata +231 -0
@@ -0,0 +1,88 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ # 大字段分离
4
+ module ActsAsContentModeAble
5
+ def self.included base
6
+ base.class_eval do
7
+ extend ClassMethods
8
+ end
9
+ end
10
+
11
+ LOYAL_CORE_CONTENT_MODE_CONFIGS = ::LoyalCore::ConfigUtil.new(
12
+ { :key => :html, :value => 0, :desc => "HTML" },
13
+ { :key => :text, :value => 1, :desc => "TEXT" },
14
+ { :key => :markdown, :value => 2, :desc => "Markdown" }
15
+ )
16
+
17
+ module ClassMethods
18
+ def loyal_core_acts_as_content_mode_able *args
19
+ extend ContentModeClassMethods
20
+ include ContentModeInstanceMethods
21
+
22
+ # 文字不能为空
23
+ # validates_presence_of :content_text
24
+ end
25
+
26
+ module ContentModeClassMethods
27
+ def content_mode_config
28
+ LOYAL_CORE_CONTENT_MODE_CONFIGS
29
+ end
30
+ end
31
+
32
+ module ContentModeInstanceMethods
33
+ def content_mode_was_cache_reset!
34
+ @content_mode_was_cache = self.content_mode
35
+ end
36
+
37
+ # FIXME
38
+ def content_mode_was_cache
39
+ (
40
+ @content_mode_was_cache ||= self.content_mode
41
+ ).to_i
42
+ end
43
+
44
+ def content_mode_was_cache= mode
45
+ @content_mode_was_cache = mode
46
+ end
47
+
48
+ def content_mode_was_cache_changed?
49
+ self.content_mode_was_cache != self.content_mode
50
+ end
51
+
52
+ def content_mode_name= name
53
+ self.content_mode = self.class.content_mode_config.at(name).value
54
+ end
55
+
56
+ def content_mode_name
57
+ self.class.content_mode_config.item(self.content_mode).key
58
+ end
59
+
60
+ def content
61
+ case self.content_mode_name
62
+ when :html
63
+ ::LoyalCore::TextUtil.syntax_highlighter(
64
+ ::Sanitize.clean(self.content_code, ::LoyalCore.config.sanitize_config[:relaxed])
65
+ )
66
+ when :markdown
67
+ # TODO: markdown
68
+ ::LoyalCore::TextUtil.markdown(self.content_code)
69
+ when :text
70
+ self.content_text
71
+ end
72
+ end
73
+
74
+ def content= content
75
+ self.content_code = content
76
+ end
77
+
78
+ def content_text
79
+ ::Sanitize.clean(self.content_code, ::LoyalCore.config.sanitize_config[:text]) || 0
80
+ end
81
+
82
+ extend ::LoyalCore::Memoist
83
+
84
+ memoize :content_text, :content
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ module ActsAsHasAvatar
4
+ # LOYAL_CORE_AVATAR_STYLES = {
5
+ # :medium => "360x360#",
6
+ # :small => "180x180#",
7
+ # :thumb => "100x100#",
8
+ # :mini => "50x50#"
9
+ # }.freeze
10
+
11
+ def self.included(base)
12
+ base.class_eval do
13
+ extend ClassMethods
14
+ end
15
+ end
16
+
17
+ module ClassMethods
18
+ #
19
+ #
20
+ def loyal_core_acts_as_has_avatar(*args)
21
+ options = args.extract_options!
22
+
23
+ field_name = args.first || :avatar
24
+
25
+ attr_accessible field_name, :"#{field_name}_cache"
26
+ mount_uploader field_name, ::LoyalCore::AvatarUploader
27
+ end
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,43 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ module ActsAsHasPermalink
4
+ PERMALINK_REGEXP = /^[A-Za-z0-9\_]+$/.freeze
5
+
6
+ def self.included base
7
+ base.class_eval do
8
+ extend ClassMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def loyal_core_acts_as_has_permalink *args
14
+ options = args.extract_options!
15
+ field_name = args.first || :permalink
16
+
17
+ include WithSpaceMethods
18
+
19
+ if options[:with_space]
20
+ validates_format_of field_name, :with => PERMALINK_REGEXP, :unless => :space_root?, :multiline => true
21
+ validates_presence_of field_name, :unless => :space_root?
22
+
23
+ # space 格式要求正确
24
+ validates_format_of :space, :with => PERMALINK_REGEXP, :multiline => true
25
+ validates_uniqueness_of field_name, :uniqueness => {:scope => [:space]}
26
+ else
27
+ validates_format_of field_name, :with => PERMALINK_REGEXP, :multiline => true
28
+ validates_presence_of field_name
29
+
30
+ validates_uniqueness_of field_name
31
+ end
32
+ end
33
+
34
+ module WithSpaceMethods
35
+
36
+ def space_root?
37
+ self.permalink.blank?
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,90 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ module ActsAsLikeTrackAble
4
+ def self.included base
5
+ base.class_eval do
6
+ extend ClassMethods
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def loyal_core_acts_as_like_track_able *args
12
+ has_many :like_tracks, :class_name => "LoyalCore::LikeTrack", :as => :target
13
+
14
+ include InstanceMethods
15
+
16
+ scope :filter_by_liked_by, ->(user_or_user_id) do
17
+ joins(:like_tracks).where(
18
+ :"#{::LoyalCore::LikeTrack.table_name}" => {
19
+ :created_by => (user_or_user_id.is_a?(User) ? user_or_user_id.id : user_or_user_id)
20
+ }
21
+ )
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def user_like_tracks user
27
+ self.like_tracks.filter_by_creator user
28
+ end
29
+
30
+ # 是否被 ** 喜欢了?
31
+ def liked_by? user
32
+ return false if user.nil?
33
+
34
+ ::TinyCache.cache_store.fetch liked_cache_key(:user_id => user.id) do
35
+ self.user_like_tracks(user).exists?
36
+ end
37
+ end
38
+
39
+ # 被**喜欢
40
+ def liked_by! user, options={}
41
+ tmp_scope = self.user_like_tracks(user)
42
+ tmp_scope.create
43
+
44
+ self.reload
45
+
46
+ self.touch
47
+
48
+ _expire_liked_cache :user_id => user.id
49
+ end
50
+
51
+ # 取消喜欢
52
+ def undo_liked_by! user
53
+ self.user_like_tracks(user).destroy_all
54
+
55
+ self.reload
56
+
57
+ self.touch
58
+
59
+ _expire_liked_cache :user_id => user.id
60
+ end
61
+
62
+ # return:
63
+ # :like
64
+ # :undo
65
+ def touch_liked_by! user
66
+ if self.liked_by?(user)
67
+ self.undo_liked_by!(user)
68
+ :undo
69
+ else
70
+ self.liked_by!(user)
71
+ :like
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ # 使过期
78
+ def _expire_liked_cache options={}
79
+ # 刷新更新时间
80
+
81
+ ::TinyCache.cache_store.delete liked_cache_key(options)
82
+ end
83
+
84
+ def liked_cache_key options={}
85
+ "#{::TinyCache.cache_key_prefix}/ables/like_track_able/#{self.class.name}/#{self.id}/userid:#{options[:user_id]}"
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ module ActsAsNamedFilterAble
4
+ def self.included base
5
+ base.class_eval do
6
+ extend ClassMethods
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def loyal_core_acts_as_named_filter_able *args
12
+ options = args.extract_options!
13
+
14
+ field_name = args.first
15
+ config = options[:config] || ::LoyalCore::ConfigUtil.new
16
+
17
+ # color_named_filter_config
18
+ self.class.send :define_method, :"#{field_name}_named_filter_config" do
19
+ config
20
+ end
21
+
22
+ # filter_by_named_color
23
+ scope :"filter_by_named_#{field_name}", ->(*names) do
24
+ where :"#{field_name}" => config.values_at(*names)
25
+ end
26
+
27
+ # filter_named_color
28
+ define_method :"filter_named_#{field_name}" do
29
+ config.item(self.send field_name)
30
+ end
31
+
32
+ include InstanceMethods
33
+ end
34
+
35
+ module InstanceMethods
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,76 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ module ActsAsRatingTrackAble
4
+ def self.included base
5
+ base.class_eval do
6
+ extend ClassMethods
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def loyal_core_acts_as_rating_track_able *args
12
+ has_many :rating_tracks, :class_name => "LoyalCore::RatingTrack", :as => :target
13
+
14
+ include InstanceMethods
15
+ end
16
+
17
+ module InstanceMethods
18
+ def user_rating_tracks user
19
+ self.rating_tracks.filter_by_creator(user)
20
+ end
21
+
22
+ # 是否被**打分?
23
+ def rating_by?(user)
24
+ self.user_rating_tracks(user).exists?
25
+ end
26
+
27
+ # 加分
28
+ def up_rating_by!(user, score=1)
29
+ if rating_by? user
30
+ :already
31
+ else
32
+ self.rating_by! user, score
33
+ :up
34
+ end
35
+ end
36
+
37
+ # 打低分
38
+ def down_rating_by!(user, score=-1)
39
+ if rating_by? user
40
+ :already
41
+ else
42
+ self.rating_by! user, score
43
+ :down
44
+ end
45
+ end
46
+
47
+ # 用户进行打分
48
+ def rating_by!(user, score=1)
49
+ tmp_scope = self.user_rating_tracks(user)
50
+
51
+ tmp_scope.first || tmp_scope.create(
52
+ :score => score
53
+ )
54
+
55
+ _expire_rating_track_cache
56
+ end
57
+
58
+ # 取消打分
59
+ def undo_rating_by!(user)
60
+ self.user_rating_tracks(user).destroy_all
61
+
62
+ _expire_rating_track_cache
63
+ end
64
+
65
+ private
66
+
67
+ def _expire_rating_track_cache
68
+ self.reload
69
+ self.touch
70
+ end
71
+
72
+ end
73
+ end
74
+ end
75
+ end
76
+
@@ -0,0 +1,56 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ module ActsAsSkinDishAble
4
+ def self.included base
5
+ base.class_eval do
6
+ extend ExtendMethods
7
+ end
8
+ end
9
+
10
+ module ExtendMethods
11
+ def loyal_core_acts_as_skin_dish_able *args
12
+ options = args.extract_options!
13
+
14
+ has_one :skin_dish, :class_name => '::LoyalCore::Skin::Dish', :as => :target
15
+ has_one :skin_recipe, :class_name => '::LoyalCore::Skin::Recipe',
16
+ :through => :skin_dish, :source => :recipe
17
+
18
+ attr_accessible :skin_recipe_id
19
+
20
+ define_method :skin_recipe_id do
21
+ @skin_recipe_id ||= self.skin_recipe.try(:id)
22
+ end
23
+
24
+ define_method :skin_recipe_id= do |recipe_id|
25
+ @skin_recipe_id ||= recipe_id
26
+ end
27
+
28
+ before_validation do |r|
29
+ r.skin_recipe = ::LoyalCore::Skin::Recipe.find_by_id(r.skin_recipe_id)
30
+ end
31
+
32
+ define_method :skin_recipe_html do |context|
33
+ html = ''
34
+
35
+ if self.skin_recipe
36
+ if self.skin_recipe.assets_path.present?
37
+ html << context.stylesheet_link_tag(self.skin_recipe.assets_path)
38
+ end
39
+
40
+ if self.skin_recipe.stylesheet_text.present?
41
+ html += <<-HTML
42
+ <style type='text/css'>
43
+ #{self.skin_recipe.stylesheet_text}
44
+ </style>
45
+ HTML
46
+ end
47
+ end
48
+
49
+ html
50
+ end
51
+
52
+ end
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,104 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ module ActsAsTree
4
+ def self.included base
5
+ base.class_eval do
6
+ extend ClassMethods
7
+ include InstanceMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def loyal_core_acts_as_tree options={}
13
+ attr_accessible :parent_id
14
+
15
+ acts_as_nested_set options.reverse_merge(
16
+ :parent_column => 'parent_id',
17
+ :left_column => 'left_id',
18
+ :right_column => 'right_id',
19
+ :depth_column => 'depth',
20
+ :dependent => :destroy, # or :destroy
21
+ :polymorphic => false,
22
+ :counter_cache => 'children_count'
23
+ )
24
+
25
+ scope :filter_by_root, -> do
26
+ where :parent_id => nil
27
+ end
28
+
29
+ include InstanceMethods
30
+ extend ExtendsMethods
31
+ end
32
+ end
33
+
34
+ module InstanceMethods
35
+ def children?
36
+ self.children_count > 0
37
+ end
38
+
39
+ # 祖先
40
+ def tree_ancestor_ids
41
+ self.ancestors.map(&:id).uniq.sort
42
+ end
43
+
44
+ def tree_self_and_ancestor_ids
45
+ self.self_and_ancestors.map(&:id).uniq.sort
46
+ end
47
+
48
+ # 同一个父母的孩子
49
+ def tree_sibling_ids
50
+ self.siblings.map(&:id).uniq.sort
51
+ end
52
+
53
+ def tree_self_and_siblings_ids
54
+ self.self_and_siblings_ids.map(&:id).uniq.sort
55
+ end
56
+
57
+ # 子孙后代
58
+ def tree_descendant_ids
59
+ self.descendants.map(&:id).uniq.sort
60
+ end
61
+
62
+ # 子孙后代 包括自己
63
+ def tree_self_and_descendant_ids
64
+ self.self_and_descendants.map(&:id).uniq.sort
65
+ end
66
+ end
67
+
68
+ module ExtendsMethods
69
+ def nested_set_options(class_or_item=nil, mover = nil)
70
+ class_or_item ||= self
71
+
72
+ if class_or_item.is_a? Array
73
+ items = class_or_item.reject { |e| !e.root? }
74
+ else
75
+ class_or_item = class_or_item.roots if class_or_item.respond_to?(:scoped)
76
+ items = Array(class_or_item)
77
+ end
78
+
79
+ result = []
80
+
81
+ items.each do |root|
82
+ result += root.class.associate_parents(root.self_and_descendants).map do |i|
83
+ if mover.nil? || mover.new_record? || mover.move_possible?(i)
84
+ [yield(i), i.id]
85
+ end
86
+ end.compact
87
+ end
88
+
89
+ result
90
+ end
91
+
92
+ def nested_set_select_options(class_or_item=nil, mover=nil, options={})
93
+ result = []
94
+
95
+ result << ['- - - -', nil] if options[:include_blank]
96
+
97
+ result += nested_set_options(class_or_item, mover) do |item|
98
+ "#{'- ' * item.level}#{item.name}"
99
+ end
100
+ end
101
+ end
102
+
103
+ end
104
+ end
@@ -0,0 +1,39 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # usage:
3
+ # include LoyalCore::ActsAsUUIDFul
4
+ module LoyalCore
5
+ module ActsAsUUIDFul
6
+ def self.included(base)
7
+ base.class_eval do
8
+ extend ClassMethods
9
+ include InstanceMethods
10
+
11
+ after_initialize do |r|
12
+ r.impl_loyal_core_uuid_generate
13
+ end
14
+
15
+ validates_uniqueness_of :uuid
16
+ validates_presence_of :uuid
17
+ end
18
+ end
19
+
20
+ module ClassMethods
21
+ def uuid_rebuild!
22
+ self.all.each do |item|
23
+ item.impl_loyal_core_uuid_generate false
24
+ item.save
25
+ end
26
+ end
27
+ end
28
+
29
+ module InstanceMethods
30
+ def impl_loyal_core_uuid_generate(force=false)
31
+ begin
32
+ self.uuid = ::SecureRandom.uuid if force || self.uuid.blank?
33
+ rescue
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,33 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ module ActsAsViewsCountAble
4
+ def self.included base
5
+ base.class_eval do
6
+ extend ClassMethods
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ # 具备计数功能
12
+ # views_count
13
+ def loyal_core_acts_as_views_count_able *args
14
+ options = ::LoyalCore::ArrayUtil.extract_options!(args)
15
+
16
+ define_method :increment_views_count! do
17
+ Rails.logger.debug " -> loyal_core_acts_as_views_count_able: #{self.id} increment_views_count!"
18
+ self.increment(:views_count)
19
+
20
+ # 刷新更新时间
21
+ self.refresh_tiny_cache_updated_at
22
+ self.write_tiny_cache # 写入缓存
23
+
24
+ # 频率间隔, 默认为10次
25
+ if self.views_count % (options[:interval] || 10).to_i == 0
26
+ self.class.update_all({:views_count => self.views_count}, :id => self.id)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,35 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module LoyalCore
3
+ autoload :ActsAsBitAble, "#{File.dirname(__FILE__)}/acts/bit_able"
4
+ autoload :ActsAsTree, "#{File.dirname(__FILE__)}/acts/tree"
5
+ autoload :ActsAsHasAvatar, "#{File.dirname(__FILE__)}/acts/has_avatar"
6
+ autoload :ActsAsHasPermalink, "#{File.dirname(__FILE__)}/acts/has_permalink"
7
+ autoload :ActsAsContentModeAble, "#{File.dirname(__FILE__)}/acts/content_mode_able"
8
+ autoload :ActsAsContentDepartAble, "#{File.dirname(__FILE__)}/acts/content_depart_able"
9
+
10
+ autoload :ActsAsUUIDFul, "#{File.dirname(__FILE__)}/acts/uuid_ful"
11
+ autoload :ActsAsRatingTrackAble, "#{File.dirname(__FILE__)}/acts/rating_track_able"
12
+ autoload :ActsAsLikeTrackAble, "#{File.dirname(__FILE__)}/acts/like_track_able"
13
+
14
+ autoload :ActsAsViewsCountAble, "#{File.dirname(__FILE__)}/acts/views_count_able"
15
+ autoload :ActsAsNamedFilterAble, "#{File.dirname(__FILE__)}/acts/named_filter_able"
16
+
17
+ autoload :ActsAsSkinDishAble, "#{File.dirname(__FILE__)}/acts/skin_dish_able"
18
+ end
19
+
20
+ if defined?(ActiveRecord::Base)
21
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsBitAble
22
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsTree
23
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsHasAvatar
24
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsHasPermalink
25
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsContentModeAble
26
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsContentDepartAble
27
+
28
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsRatingTrackAble
29
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsLikeTrackAble
30
+
31
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsViewsCountAble
32
+
33
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsNamedFilterAble
34
+ ::ActiveRecord::Base.send :include, ::LoyalCore::ActsAsSkinDishAble
35
+ end