page_parts 0.0.2 → 0.0.3

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.
@@ -28,11 +28,11 @@ module PageParts
28
28
  self.page_parts_definitions[:keys] << method_name
29
29
 
30
30
  define_method(method_name) do
31
- page_part(method_name).try(:content)
31
+ _page_part(method_name).try(:content)
32
32
  end
33
33
 
34
34
  define_method("#{method_name}=") do |value|
35
- record = page_part(method_name)
35
+ record = _page_part(method_name)
36
36
  record.content = value
37
37
  end
38
38
  end
@@ -40,33 +40,36 @@ module PageParts
40
40
  end
41
41
  end
42
42
 
43
- module InstanceMethods
43
+ # Find page part by key or build new record
44
+ def find_or_build_page_part(attr_name)
45
+ key = normalize_page_part_key(attr_name)
46
+ #self.page_parts.where(:key => key).first || self.page_parts.build(:key => key)
47
+ self.page_parts.detect { |record| record.key.to_s == key } || self.page_parts.build(:key => key)
48
+ end
49
+
50
+ def reload(options = nil)
51
+ @cached_page_parts = nil
52
+ super
53
+ end
54
+
55
+ protected
44
56
 
45
- # Find page part by key or build new record
46
- def find_or_build_page_part(attr_name)
47
- key = normalize_page_part_key(attr_name)
48
- self.page_parts.where(:key => key).first || self.page_parts.build(:key => key)
49
- end
50
-
51
57
  # Save page parts records into one hash
52
- def page_part(attr_name)
58
+ def _page_part(attr_name)
53
59
  key = normalize_page_part_key(attr_name)
54
- @page_part ||= {}
55
- @page_part[key] ||= find_or_build_page_part(key)
60
+ @cached_page_parts ||= {}
61
+ @cached_page_parts[key] ||= find_or_build_page_part(key)
56
62
  end
57
-
58
- protected
59
-
60
- def normalize_page_part_key(value)
61
- key = value.to_s.downcase.strip
62
-
63
- unless self.page_parts_definitions[:keys].include?(key)
64
- raise NoMethodError, "Page part #{key} not registered"
65
- end
66
-
67
- key
63
+
64
+ def normalize_page_part_key(value)
65
+ key = value.to_s.downcase.strip
66
+
67
+ unless self.page_parts_definitions[:keys].include?(key)
68
+ raise NoMethodError, "Page part #{key} not registered"
68
69
  end
70
+
71
+ key
72
+ end
69
73
 
70
- end
71
74
  end
72
75
  end
@@ -1,3 +1,3 @@
1
1
  module PageParts
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -782,3 +782,72 @@ Migrating to CreatePosts (20120124093705)
782
782
  SQL (0.3ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", nil], ["created_at", Tue, 24 Jan 2012 09:45:43 UTC +00:00], ["key", "content_ru"], ["partable_id", 1], ["partable_type", "Post"], ["updated_at", Tue, 24 Jan 2012 09:45:43 UTC +00:00]]
783
783
   (0.0ms) RELEASE SAVEPOINT active_record_1
784
784
   (0.1ms) SELECT COUNT(*) FROM "page_parts"
785
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
786
+ Migrating to CreatePageParts (20111226144515)
787
+  (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
788
+ Migrating to CreateCategories (20111226131910)
789
+ Migrating to CreatePosts (20120124093705)
790
+  (0.1ms) SAVEPOINT active_record_1
791
+  (0.2ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'sidebar' AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" IS NULL) LIMIT 1
792
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'content' AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" IS NULL) LIMIT 1
793
+ SQL (1.1ms) INSERT INTO "categories" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["title", "Test category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
794
+ SQL (0.2ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "Sidebar"], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "sidebar"], ["partable_id", 1], ["partable_type", "Category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
795
+ SQL (0.1ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "Main"], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "content"], ["partable_id", 1], ["partable_type", "Category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
796
+  (0.0ms) RELEASE SAVEPOINT active_record_1
797
+ Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", 1]]
798
+ PagePart Load (0.1ms) SELECT "page_parts".* FROM "page_parts" WHERE "page_parts"."partable_id" = 1 AND "page_parts"."partable_type" = 'Category'
799
+  (0.1ms) SELECT COUNT(*) FROM "page_parts"
800
+  (0.0ms) SAVEPOINT active_record_1
801
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'sidebar' AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" IS NULL) LIMIT 1
802
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'content' AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" IS NULL) LIMIT 1
803
+ SQL (0.2ms) INSERT INTO "categories" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["title", "Test category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
804
+ SQL (0.3ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "Sidebar content"], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "sidebar"], ["partable_id", 1], ["partable_type", "Category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
805
+ SQL (0.3ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", nil], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "content"], ["partable_id", 1], ["partable_type", "Category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
806
+  (0.0ms) RELEASE SAVEPOINT active_record_1
807
+  (0.1ms) SELECT COUNT(*) FROM "page_parts"
808
+  (0.0ms) SAVEPOINT active_record_1
809
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'sidebar' AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" IS NULL) LIMIT 1
810
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'content' AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" IS NULL) LIMIT 1
811
+ SQL (0.2ms) INSERT INTO "categories" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["title", "Test category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
812
+ SQL (0.2ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "Sidebar"], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "sidebar"], ["partable_id", 1], ["partable_type", "Category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
813
+ SQL (0.1ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "Main"], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "content"], ["partable_id", 1], ["partable_type", "Category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
814
+  (0.0ms) RELEASE SAVEPOINT active_record_1
815
+ Category Load (0.0ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", 1]]
816
+ PagePart Load (0.1ms) SELECT "page_parts".* FROM "page_parts" WHERE "page_parts"."partable_id" = 1 AND "page_parts"."partable_type" = 'Category'
817
+  (0.0ms) SAVEPOINT active_record_1
818
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'sidebar' AND "page_parts"."id" != 1 AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" = 1) LIMIT 1
819
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'content' AND "page_parts"."id" != 2 AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" = 1) LIMIT 1
820
+  (0.2ms) UPDATE "page_parts" SET "content" = 'Sidebar 2', "updated_at" = '2012-01-26 12:37:57.392267' WHERE "page_parts"."id" = 1
821
+  (0.1ms) UPDATE "page_parts" SET "content" = 'Main 2', "updated_at" = '2012-01-26 12:37:57.393314' WHERE "page_parts"."id" = 2
822
+  (0.0ms) RELEASE SAVEPOINT active_record_1
823
+ Category Load (0.0ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", 1]]
824
+ PagePart Load (0.1ms) SELECT "page_parts".* FROM "page_parts" WHERE "page_parts"."partable_id" = 1 AND "page_parts"."partable_type" = 'Category'
825
+  (0.0ms) SAVEPOINT active_record_1
826
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'somekey' AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" = 1) LIMIT 1
827
+ SQL (0.3ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", nil], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "somekey"], ["partable_id", 1], ["partable_type", "Category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
828
+  (0.0ms) RELEASE SAVEPOINT active_record_1
829
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = '' AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" = 1) LIMIT 1
830
+  (0.0ms) SAVEPOINT active_record_1
831
+ SQL (0.3ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", nil], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "test"], ["partable_id", 1], ["partable_type", "Category"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
832
+  (0.0ms) RELEASE SAVEPOINT active_record_1
833
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'test' AND "page_parts"."partable_type" = 'Category' AND "page_parts"."partable_id" = 1) LIMIT 1
834
+  (0.0ms) SAVEPOINT active_record_1
835
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'title_ru' AND "page_parts"."partable_type" = 'Post' AND "page_parts"."partable_id" IS NULL) LIMIT 1
836
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'content_uk' AND "page_parts"."partable_type" = 'Post' AND "page_parts"."partable_id" IS NULL) LIMIT 1
837
+ SQL (0.5ms) INSERT INTO "posts" ("created_at", "is_visible", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["is_visible", true], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
838
+ SQL (0.3ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "Sidebar"], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "title_ru"], ["partable_id", 1], ["partable_type", "Post"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
839
+ SQL (0.2ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "Main"], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "content_uk"], ["partable_id", 1], ["partable_type", "Post"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
840
+  (0.1ms) RELEASE SAVEPOINT active_record_1
841
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 1]]
842
+ PagePart Load (0.1ms) SELECT "page_parts".* FROM "page_parts" WHERE "page_parts"."partable_id" = 1 AND "page_parts"."partable_type" = 'Post'
843
+  (0.1ms) SELECT COUNT(*) FROM "page_parts"
844
+  (0.0ms) SAVEPOINT active_record_1
845
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'title_ru' AND "page_parts"."partable_type" = 'Post' AND "page_parts"."partable_id" IS NULL) LIMIT 1
846
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'title_en' AND "page_parts"."partable_type" = 'Post' AND "page_parts"."partable_id" IS NULL) LIMIT 1
847
+  (0.1ms) SELECT 1 FROM "page_parts" WHERE ("page_parts"."key" = 'content_ru' AND "page_parts"."partable_type" = 'Post' AND "page_parts"."partable_id" IS NULL) LIMIT 1
848
+ SQL (0.2ms) INSERT INTO "posts" ("created_at", "is_visible", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["is_visible", true], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
849
+ SQL (0.2ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "Title ru"], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "title_ru"], ["partable_id", 1], ["partable_type", "Post"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
850
+ SQL (0.1ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "Title en"], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "title_en"], ["partable_id", 1], ["partable_type", "Post"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
851
+ SQL (0.2ms) INSERT INTO "page_parts" ("content", "created_at", "key", "partable_id", "partable_type", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", nil], ["created_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00], ["key", "content_ru"], ["partable_id", 1], ["partable_type", "Post"], ["updated_at", Thu, 26 Jan 2012 12:37:57 UTC +00:00]]
852
+  (0.0ms) RELEASE SAVEPOINT active_record_1
853
+  (0.1ms) SELECT COUNT(*) FROM "page_parts"
@@ -18,8 +18,8 @@ class CategoryTest < ActiveSupport::TestCase
18
18
  value = "Sidebar content"
19
19
  @category.sidebar = value
20
20
 
21
- assert_equal @category.page_part(:sidebar).content, value
22
- assert_equal @category.page_part(:content).content, nil
21
+ assert_equal @category.send(:_page_part, :sidebar).content, value
22
+ assert_equal @category.send(:_page_part, :content).content, nil
23
23
 
24
24
  assert_difference('PagePart.count', 2) do
25
25
  @category.save
@@ -37,6 +37,24 @@ class CategoryTest < ActiveSupport::TestCase
37
37
  assert_equal @category.content, "Main"
38
38
  end
39
39
 
40
+ test "should update page parts" do
41
+ @category.sidebar = "Sidebar"
42
+ @category.content = "Main"
43
+ @category.save
44
+
45
+ @category.reload
46
+
47
+ assert_equal @category.sidebar, "Sidebar"
48
+ assert_equal @category.content, "Main"
49
+
50
+ @category.update_attributes(:sidebar => "Sidebar 2", :content => "Main 2")
51
+
52
+ @category.reload
53
+
54
+ assert_equal @category.sidebar, "Sidebar 2"
55
+ assert_equal @category.content, "Main 2"
56
+ end
57
+
40
58
  test "should raise error on not registered page part" do
41
59
  assert_raise(NoMethodError) do
42
60
  @category.wrong_method
@@ -17,9 +17,9 @@ class PostTest < ActiveSupport::TestCase
17
17
  value_en = "Title en"
18
18
  @post.title_en = value_en
19
19
 
20
- assert_equal @post.page_part(:title_ru).content, value_ru
21
- assert_equal @post.page_part(:title_en).content, value_en
22
- assert_equal @post.page_part(:content_ru).content, nil
20
+ assert_equal @post.send(:_page_part, :title_ru).content, value_ru
21
+ assert_equal @post.send(:_page_part, :title_en).content, value_en
22
+ assert_equal @post.send(:_page_part, :content_ru).content, nil
23
23
 
24
24
  assert_difference('PagePart.count', 3) do
25
25
  @post.save
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: page_parts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-24 00:00:00.000000000 Z
12
+ date: 2012-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &80855800 !ruby/object:Gem::Requirement
16
+ requirement: &74313950 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *80855800
24
+ version_requirements: *74313950
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &80854830 !ruby/object:Gem::Requirement
27
+ requirement: &74313740 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *80854830
35
+ version_requirements: *74313740
36
36
  description: Aimbulance CMS
37
37
  email:
38
38
  - galeta.igor@gmail.com