redde 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbde0641c49836754b4070b0113f4298222340cb
4
- data.tar.gz: 5b2da2600e17118d0515351e7c105598f953c486
3
+ metadata.gz: 415d13d5581f203913ddbe74ba59d6fed19125a8
4
+ data.tar.gz: 00b8a99f56c2ad97b1d91bb1684529e389237126
5
5
  SHA512:
6
- metadata.gz: 9b3af10712fe15ee2e2871a3ea20534af14d7c8d296e3d4a9ba62b34c19b0e43b1b6ab15618b91073efcd88cc6b9552a4c4dabad7d454c0a3aedc2753c7a6148
7
- data.tar.gz: 6767bae525444c8e97fc53c81d416bfee50ebd673184ee154ef085dde15fcd6c0493a0e82b67124f5fff4d3e0cf327ac0fe7d97a9c70ac6bebc0bc56f08aad94
6
+ metadata.gz: 11ad3b74c37115cd4af58828c23e65fd9c395aedb5e8b2eafaaf719e42193a3da26464dac95ce2d8660fe803eff53fe7d853d97b2da5cdbaeb2506da6984a9f4
7
+ data.tar.gz: 3555c40b264141d970ff35cdff80ec0e06ff095ff8891912f3ee23de7ad920bdca83769bdc60354af9d9d2354e365abb85ef60739b42b864ea21671687485a6f
data/README.md CHANGED
@@ -37,13 +37,50 @@ To set admin login layout you need to modify application controller:
37
37
  end
38
38
 
39
39
  To generate admin views and controller for a model, enter:
40
-
40
+
41
41
  rails g redde:scaffold ModelNames
42
42
 
43
43
  Add `admin.scss` and `admin.js` to assets precompilation in config/production.rb:
44
44
 
45
45
  config.assets.precompile += %w( admin.js admin.css )
46
-
46
+
47
+ ## UrlGenerator
48
+
49
+ `Redde::UrlGenerator` - is a small lib to convert title and id to combine url, used in `to_param` method.
50
+
51
+ Usage example:
52
+
53
+ generator = Redde::UrlGenerator.new(1, 'тестовый заголовок $%##@$@#$')
54
+ generator.url
55
+ => '1-testovyy-zagolovok'
56
+
57
+ ## Sluggable
58
+
59
+ `Sluggable` is used to include into model with permanent slugs (similar to permalink).
60
+
61
+ Your ActiveRecord model should have `slug` field and title field.
62
+
63
+ You can set title field by setting `TITLE_FIELD` to symbol of method. This method will be used to generate `slug`.
64
+ If `TITLE_FIELD` constant is not set, `:title` symbol will be used insted.
65
+
66
+ Usage example:
67
+
68
+ Book should have title and slug fields.
69
+
70
+ class Book < ActiveRecord::Base
71
+ TITLE_SYMBOL = :name
72
+ include Redde::Sluggable
73
+ validates :name, presence: true
74
+ end
75
+
76
+ b = Book.new(name: 'Тестовая книга')
77
+ b.save
78
+ b.slug
79
+ => 'testovaya-kniga'
80
+
81
+ b.to_param
82
+ => '1-testovaya-kniga'
83
+
47
84
  ## Добавление фотографий
48
85
 
49
86
  rails g redde:photo
@@ -67,7 +104,7 @@ Add `admin.scss` and `admin.js` to assets precompilation in config/production.rb
67
104
  Не забудьте добавить полиморфную связь
68
105
 
69
106
  has_many :photos, dependent: :destroy, as: :imageable
70
-
107
+
71
108
  создаст scaffold для модели Photo с полиморфной связью
72
109
 
73
110
  ## Gemset dependenсies
@@ -79,7 +116,7 @@ Somehow, some dependencies are not initialized inside rails apps. If you get err
79
116
  gem 'haml-rails'
80
117
  gem 'russian'
81
118
  gem 'devise'
82
-
119
+
83
120
  Its highly possible, that you will not have any problems with gems
84
121
 
85
122
  ## Autoprefixer note for development mode
@@ -107,7 +144,7 @@ Its neccessary to have joined asset files, change assets debug to false in `conf
107
144
  To use styles for the WYSIWYG editor, add its styles to precompile in `config/production.rb`:
108
145
 
109
146
  config.assets.precompile += %w( redactor/wym.css )
110
-
147
+
111
148
  ## Sortable
112
149
 
113
150
  If you have field `position` of integer type in your model, generator will add special column as a hook for sort.
@@ -5,10 +5,12 @@ class CreatePhotos < ActiveRecord::Migration
5
5
  t.string :imageable_type
6
6
  t.integer :position
7
7
  t.string :src
8
+ t.string :token
8
9
 
9
10
  t.timestamps
10
11
  end
11
12
  add_index :photos, :imageable_id
12
13
  add_index :photos, :imageable_type
14
+ add_index :photos, :token
13
15
  end
14
16
  end
@@ -0,0 +1,28 @@
1
+ require 'redde/url_generator'
2
+
3
+ module Redde::Sluggable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ validates :slug, format: { with: /\A[A-z0-9_-]*\z/i, message: 'имеет неверный формат, разрешены английские символы, знак подчеркивания "_" и дефис "-"'}, presence: true
8
+ before_validation :set_slug
9
+ end
10
+
11
+ def set_slug
12
+ self.slug = Redde::UrlGenerator.new(self, title_field).formatted_name.downcase unless slug.present?
13
+ end
14
+
15
+ def title_field
16
+ send(title_symbol)
17
+ end
18
+
19
+ def title_symbol
20
+ self.class::TITLE_SYMBOL
21
+ rescue
22
+ :title
23
+ end
24
+
25
+ def to_param
26
+ "#{id}-#{slug.try(:downcase)}"
27
+ end
28
+ end
@@ -1,6 +1,4 @@
1
- # coding: utf-8
2
-
3
- class UrlGenerator
1
+ class Redde::UrlGenerator
4
2
  attr_reader :id, :name
5
3
  def initialize(id, name = nil)
6
4
  @id = id
@@ -8,7 +6,8 @@ class UrlGenerator
8
6
  end
9
7
 
10
8
  def url
11
- "#{id}#-{formatted_name}".downcase
9
+ return "#{id}-#{formatted_name}".downcase if formatted_name.present?
10
+ id.to_s
12
11
  end
13
12
 
14
13
  def translitted_name
data/lib/redde/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Redde
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
@@ -0,0 +1,13 @@
1
+ module WithPhoto
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ attr_accessor :tokens
6
+ has_many :photos, dependent: :destroy, as: :imageable
7
+ after_create :assign_photos
8
+ end
9
+
10
+ def assign_photos
11
+ Photo.where(token: tokens || []).update_all(imageable_id: self.id, imageable_type: self.class.name)
12
+ end
13
+ end
data/lib/redde.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'redde/version'
2
+ require 'redde/sluggable'
3
+ require 'redde/with_photo'
2
4
  module Redde
3
5
  require 'generators/redde/layout/layout_generator'
4
6
  require 'generators/redde/scaffold/scaffold_generator'
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ class DummyClass
4
+ include ActiveModel::Model
5
+ include ActiveModel::Validations
6
+ include ActiveRecord::Callbacks
7
+ include Redde::Sluggable
8
+
9
+ attr_accessor :id, :title, :slug
10
+
11
+ def id
12
+ 3
13
+ end
14
+
15
+ def title
16
+ 'hey ho'
17
+ end
18
+ end
19
+
20
+
21
+ class DummyClassWithTitle < DummyClass
22
+ include Redde::Sluggable
23
+ TITLE_SYMBOL = :name
24
+
25
+ def name
26
+ 'super name'
27
+ end
28
+ end
29
+
30
+ describe Redde::Sluggable do
31
+ let(:title) { DummyClass.new }
32
+ let(:name) { DummyClassWithTitle.new }
33
+
34
+ it 'sets slug for model with title' do
35
+ title.valid?
36
+ expect(title.slug).to eq 'hey-ho'
37
+ end
38
+
39
+ it 'sets slug for model with name' do
40
+ name.valid?
41
+ expect(name.slug).to eq 'super-name'
42
+ end
43
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Redde::UrlGenerator do
4
+ let(:url) { Redde::UrlGenerator.new(1, 'тестовый заголовок $%##@$@#$') }
5
+
6
+ it 'generates valid translitted name' do
7
+ expect(url.translitted_name).to eq 'testovyy-zagolovok'
8
+ end
9
+
10
+ it 'generates valid url if name not present' do
11
+ url = Redde::UrlGenerator.new(1)
12
+ expect(url.url).to eq '1'
13
+ end
14
+
15
+ it 'generates valid url' do
16
+ expect(url.url).to eq '1-testovyy-zagolovok'
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redde
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Bovykin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-19 00:00:00.000000000 Z
12
+ date: 2014-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jquery-rails
@@ -245,8 +245,10 @@ files:
245
245
  - lib/generators/redde/scaffold/templates/edit.html.haml
246
246
  - lib/generators/redde/scaffold/templates/index.html.haml
247
247
  - lib/redde.rb
248
+ - lib/redde/sluggable.rb
249
+ - lib/redde/url_generator.rb
248
250
  - lib/redde/version.rb
249
- - lib/url_generator.rb
251
+ - lib/redde/with_photo.rb
250
252
  - redde.gemspec
251
253
  - spec/dummy/.rspec
252
254
  - spec/dummy/README.rdoc
@@ -295,6 +297,8 @@ files:
295
297
  - spec/generators/layout_generator_spec.rb
296
298
  - spec/generators/photo_generator_spec.rb
297
299
  - spec/generators/scaffold_generator_spec.rb
300
+ - spec/models/sluggable_spec.rb
301
+ - spec/models/url_generator_spec.rb
298
302
  - spec/spec_helper.rb
299
303
  homepage: http://github.com/redde/redde
300
304
  licenses:
@@ -368,4 +372,6 @@ test_files:
368
372
  - spec/generators/layout_generator_spec.rb
369
373
  - spec/generators/photo_generator_spec.rb
370
374
  - spec/generators/scaffold_generator_spec.rb
375
+ - spec/models/sluggable_spec.rb
376
+ - spec/models/url_generator_spec.rb
371
377
  - spec/spec_helper.rb