link_to_action 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  # LinkToAction
5
5
 
6
- Set of helpers: link_to_new, link_to_edit, link_to_destroy
6
+ Set of helpers: link_to_new, link_to_show, link_to_edit, link_to_destroy
7
7
 
8
8
  Bonus helper: link_to_back
9
9
 
@@ -12,25 +12,31 @@ Bonus helper: link_to_back
12
12
  Rails scaffold-generated links are not DRY, and it becomes even worse,
13
13
  when someone tries to make them I18n-friendly (they are not by default).
14
14
 
15
- If you have twitter-bootstrap installed, and want to make your links looks like
15
+ If you have twitter-bootstrap installed, and want to make your links look like
16
16
  a buttons, there will be another addition, 'class' option. To make these buttons
17
17
  really pretty, you will also need icons.
18
18
 
19
19
  So how I18n-friendly button-style link with icon looks like in a view code?
20
20
  Well, it can look like this:
21
21
 
22
- <%= link_to raw("<i class=\"icon-edit\"></i> #{t(:edit)}"), edit_comment_path(@comment), class: 'btn' %>
22
+ ```erb
23
+ <%= link_to raw("<i class=\"icon-edit\"></i> #{t(:edit)}"), edit_comment_path(@comment), class: 'btn' %>
24
+ ```
23
25
 
24
26
  And scaffolded code was:
25
27
 
26
- <%= link_to 'Edit', edit_comment_path(@comment) %>
28
+ ```erb
29
+ <%= link_to 'Edit', edit_comment_path(@comment) %>
30
+ ```
27
31
 
28
32
  Even in simple scaffolded link words 'edit' and 'comment' was repeated twice.
29
33
  In more complex example word 'edit' was written three times.
30
34
 
31
35
  Using link_to_action gem, you can avoid those duplication completely:
32
36
 
33
- link_to_edit @comment
37
+ ```erb
38
+ <%= link_to_edit @comment %>
39
+ ```
34
40
 
35
41
  That's all for this link, but not all for this gem. It configurable, it can be
36
42
  tuned up to suit other than twitter-bootstrap CSS frameworks (which is off by
@@ -56,7 +62,7 @@ Add initializer:
56
62
 
57
63
  $ rails generate link_to_action:install
58
64
 
59
- Edit config/initializers/link_to_action.rb. Note that CSS classes, icons and
65
+ Edit `config/initializers/link_to_action.rb`. Note that CSS classes, icons and
60
66
  cancan are disabled by default.
61
67
 
62
68
  ## Usage
@@ -65,12 +71,72 @@ In general:
65
71
 
66
72
  link_to_{action} object, options
67
73
 
68
- Per action:
69
-
70
- link_to_new MyModel
71
- link_to_edit @my_instance
72
- link_to_destroy @my_instance
73
- link_to_back
74
+ Per action, with commented-out link_to equivalents:
75
+
76
+ ```ruby
77
+ link_to_new MyModel # link_to 'New MyModel', new_my_model_path
78
+ link_to_show @my_model # link_to @my_model.name, my_model_path(@my_model)
79
+ link_to_edit @my_model # link_to 'Edit MyModel', edit_my_model_path(@my_model)
80
+ link_to_destroy @my_model # link_to 'Delete MyModel', my_model_path(@my_model),
81
+ # method: :delete, data: { confirm: 'Are you sure?' }
82
+ link_to_back # link_to :back
83
+ ```
84
+
85
+ `link_to_show` tries some methods (by default :name, :title, and :to_s) to get
86
+ name for the link. Other helpers uses I18n to get link names.
87
+
88
+ ## I18n
89
+
90
+ `link_to_action` comes with I18n preconfigured, nested in `helpers.link_to`
91
+ section. Custom (or localized) model names can be added to `activerecord.models`
92
+ section.
93
+
94
+ ```yml
95
+ en:
96
+ activerecord:
97
+ models:
98
+ my_model: 'My Awesome Model'
99
+ helpers:
100
+ link_to:
101
+ new: 'New %{model}'
102
+ edit: 'Edit %{model}'
103
+ destroy: 'Delete %{model}'
104
+ destroy_confirm: 'Are you sure?'
105
+ back: 'Back'
106
+ ```
107
+
108
+ ## Global CSS styles and icons
109
+
110
+ Look to `config/initializers/link_to_action.rb` for global options. Cancan also
111
+ goes here.
112
+
113
+ ## Per-link options
114
+
115
+ ```ruby
116
+ :name # overwrites default name of the link
117
+ :class # overwrites or appends class list
118
+ :size # size of the button-like link
119
+ :params # hash for to polymorphic_path, see examples
120
+ :icon # Font-Awesome icon
121
+ :icon_size # Size of icon
122
+ :icon_swap # Swap default position of icon left-right
123
+ ```
124
+
125
+ `link_to_show` options:
126
+ ```ruby
127
+ :name # overwrites default name of the link
128
+ :send # method to sent to object to get name
129
+ ```
130
+
131
+ Examples
132
+
133
+ ```ruby
134
+ # Link with parameters
135
+ link_to_new Comment, params: {user_id: 1} # /comments/new?user_id=1
136
+
137
+ # Nested link
138
+ link_to_new [ @user, Comment ] # /users/1/comments/new
139
+ ```
74
140
 
75
141
  ## Contributing
76
142
 
@@ -10,6 +10,10 @@ module LinkToAction
10
10
  def copy_initializers
11
11
  copy_file 'link_to_action.rb', 'config/initializers/link_to_action.rb'
12
12
  end
13
+
14
+ def copy_locale_file
15
+ copy_file 'en.yml', 'config/locales/link_to_action.en.yml'
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -38,4 +38,7 @@ LinkToAction.setup do |config|
38
38
  # config.size_class_large = 'btn-large'
39
39
  # config.size_class_small = 'btn-small'
40
40
  # config.size_class_mini = 'btn-mini'
41
+
42
+ # Methods for link_to_show
43
+ # config.show_methods = [ :name, :title, :to_s ]
41
44
  end
@@ -20,6 +20,16 @@ module LinkToAction::Helpers
20
20
  link_to_action :back, nil, options
21
21
  end
22
22
 
23
+ def link_to_show(object, options = {})
24
+ name = options.delete(:name)
25
+ send = options.delete(:send)
26
+ name = object.send(send) if send
27
+ LinkToAction.show_methods.each do |m|
28
+ name = object.try(m) unless name
29
+ end
30
+ link_to name, object, options
31
+ end
32
+
23
33
  # TODO: Move to separate module to avoid clashes
24
34
  private
25
35
 
@@ -34,11 +44,11 @@ module LinkToAction::Helpers
34
44
  classes = if LinkToAction.classes_append
35
45
  classes.concat [ options[:class] ]
36
46
  else
37
- options[:class]
47
+ [ options[:class] ]
38
48
  end
39
49
  end
40
- classes = [ classes ,size_class(size) ].flatten.compact.join(' ')
41
- classes.strip unless classes.blank?
50
+ classes = classes.concat([ size_class(size) ]).compact.join(' ')
51
+ classes unless classes.blank?
42
52
  end
43
53
 
44
54
  def size_class(size)
@@ -1,3 +1,3 @@
1
1
  module LinkToAction
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -62,6 +62,10 @@ module LinkToAction
62
62
 
63
63
  mattr_accessor :size_class_mini
64
64
  @@size_class_mini = 'btn-mini'
65
+
66
+ mattr_accessor :show_methods
67
+ @@show_methods = [ :name, :title, :to_s ]
68
+
65
69
  def self.setup
66
70
  yield self
67
71
  end
@@ -21,6 +21,7 @@ class DefaultValuesTest < ActiveSupport::TestCase
21
21
  size_class_large: 'btn-large',
22
22
  size_class_small: 'btn-small',
23
23
  size_class_mini: 'btn-mini',
24
+ show_methods: [ :name, :title, :to_s ]
24
25
  }
25
26
 
26
27
  DEFAULT_VALUES.each do |key, value|
@@ -0,0 +1,5 @@
1
+ class AddLoginToUsers < ActiveRecord::Migration
2
+ def change
3
+ add_column :users, :login, :string
4
+ end
5
+ end
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 20121003103958) do
14
+ ActiveRecord::Schema.define(:version => 20121005032141) do
15
15
 
16
16
  create_table "comments", :force => true do |t|
17
17
  t.string "name"
@@ -26,6 +26,7 @@ ActiveRecord::Schema.define(:version => 20121003103958) do
26
26
  t.string "name"
27
27
  t.datetime "created_at", :null => false
28
28
  t.datetime "updated_at", :null => false
29
+ t.string "login"
29
30
  end
30
31
 
31
32
  end
Binary file
data/test/helpers_test.rb CHANGED
@@ -11,6 +11,16 @@ class HelperTest < ActionView::TestCase
11
11
  link_to_new([@user, Comment])
12
12
  end
13
13
 
14
+ test 'link_to_show' do
15
+ assert_equal "<a href=\"/users/1\">#{@user.name}</a>",
16
+ link_to_show(@user)
17
+ end
18
+
19
+ test 'link_to_show using custom method' do
20
+ assert_equal "<a href=\"/users/1\">#{@user.login}</a>",
21
+ link_to_show(@user, send: :login)
22
+ end
23
+
14
24
  test 'link_to_edit' do
15
25
  assert_equal "<a href=\"/users/1/edit\">Edit My User</a>", link_to_edit(@user)
16
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: link_to_action
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
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-10-04 00:00:00.000000000 Z
12
+ date: 2012-10-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &20622580 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 3.2.8
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *20622580
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.8
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: sqlite3
27
- requirement: &20640700 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *20640700
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: simplecov
38
- requirement: &20652780 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *20652780
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: debugger
49
- requirement: &20650660 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *20650660
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  description: link_to for specific actions
59
79
  email:
60
80
  - denis.peplin@gmail.com
@@ -107,6 +127,7 @@ files:
107
127
  - test/dummy/config/routes.rb
108
128
  - test/dummy/db/migrate/20121003103906_create_users.rb
109
129
  - test/dummy/db/migrate/20121003103958_create_comments.rb
130
+ - test/dummy/db/migrate/20121005032141_add_login_to_users.rb
110
131
  - test/dummy/db/schema.rb
111
132
  - test/dummy/db/test.sqlite3
112
133
  - test/dummy/lib/assets/.gitkeep
@@ -120,8 +141,6 @@ files:
120
141
  - test/link_to_action_test.rb
121
142
  - test/support/misc_helpers.rb
122
143
  - test/test_helper.rb
123
- - test/dummy/log/development.log
124
- - test/dummy/log/test.log
125
144
  homepage: https://github.com/denispeplin/link_to_action
126
145
  licenses: []
127
146
  post_install_message:
@@ -136,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
155
  version: '0'
137
156
  segments:
138
157
  - 0
139
- hash: -2755518087987160038
158
+ hash: 1452578497091669240
140
159
  required_rubygems_version: !ruby/object:Gem::Requirement
141
160
  none: false
142
161
  requirements:
@@ -145,53 +164,52 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
164
  version: '0'
146
165
  segments:
147
166
  - 0
148
- hash: -2755518087987160038
167
+ hash: 1452578497091669240
149
168
  requirements: []
150
169
  rubyforge_project:
151
- rubygems_version: 1.8.11
170
+ rubygems_version: 1.8.24
152
171
  signing_key:
153
172
  specification_version: 3
154
173
  summary: Links to actions
155
174
  test_files:
156
- - test/support/misc_helpers.rb
157
175
  - test/helpers_test.rb
176
+ - test/link_to_action_test.rb
158
177
  - test/test_helper.rb
178
+ - test/support/misc_helpers.rb
179
+ - test/dummy/config.ru
180
+ - test/dummy/public/404.html
181
+ - test/dummy/public/422.html
182
+ - test/dummy/public/favicon.ico
183
+ - test/dummy/public/500.html
184
+ - test/dummy/script/rails
159
185
  - test/dummy/README.rdoc
160
- - test/dummy/config/routes.rb
161
- - test/dummy/config/locales/en.yml
162
- - test/dummy/config/environment.rb
186
+ - test/dummy/config/environments/test.rb
163
187
  - test/dummy/config/environments/development.rb
164
188
  - test/dummy/config/environments/production.rb
165
- - test/dummy/config/environments/test.rb
166
- - test/dummy/config/boot.rb
167
- - test/dummy/config/application.rb
168
- - test/dummy/config/initializers/secret_token.rb
169
- - test/dummy/config/initializers/link_to_action.rb
189
+ - test/dummy/config/environment.rb
190
+ - test/dummy/config/routes.rb
191
+ - test/dummy/config/database.yml
192
+ - test/dummy/config/locales/en.yml
170
193
  - test/dummy/config/initializers/session_store.rb
171
194
  - test/dummy/config/initializers/backtrace_silencers.rb
172
- - test/dummy/config/initializers/inflections.rb
173
195
  - test/dummy/config/initializers/mime_types.rb
196
+ - test/dummy/config/initializers/secret_token.rb
197
+ - test/dummy/config/initializers/link_to_action.rb
174
198
  - test/dummy/config/initializers/wrap_parameters.rb
175
- - test/dummy/config/database.yml
176
- - test/dummy/Rakefile
177
- - test/dummy/config.ru
178
- - test/dummy/app/assets/stylesheets/application.css
179
- - test/dummy/app/assets/javascripts/application.js
180
- - test/dummy/app/controllers/application_controller.rb
199
+ - test/dummy/config/initializers/inflections.rb
200
+ - test/dummy/config/application.rb
201
+ - test/dummy/config/boot.rb
181
202
  - test/dummy/app/helpers/application_helper.rb
182
203
  - test/dummy/app/views/layouts/application.html.erb
183
- - test/dummy/app/models/comment.rb
204
+ - test/dummy/app/controllers/application_controller.rb
205
+ - test/dummy/app/assets/javascripts/application.js
206
+ - test/dummy/app/assets/stylesheets/application.css
184
207
  - test/dummy/app/models/user.rb
185
- - test/dummy/log/development.log
186
- - test/dummy/log/test.log
208
+ - test/dummy/app/models/comment.rb
209
+ - test/dummy/Rakefile
187
210
  - test/dummy/db/test.sqlite3
188
- - test/dummy/db/schema.rb
189
- - test/dummy/db/migrate/20121003103906_create_users.rb
190
211
  - test/dummy/db/migrate/20121003103958_create_comments.rb
191
- - test/dummy/public/422.html
192
- - test/dummy/public/404.html
193
- - test/dummy/public/500.html
194
- - test/dummy/public/favicon.ico
195
- - test/dummy/script/rails
212
+ - test/dummy/db/migrate/20121003103906_create_users.rb
213
+ - test/dummy/db/migrate/20121005032141_add_login_to_users.rb
214
+ - test/dummy/db/schema.rb
196
215
  - test/default_values_test.rb
197
- - test/link_to_action_test.rb
@@ -1,25 +0,0 @@
1
- Connecting to database specified by database.yml
2
- Connecting to database specified by database.yml
3
- Connecting to database specified by database.yml
4
-  (0.1ms) select sqlite_version(*)
5
-  (4.3ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
6
-  (0.0ms) PRAGMA index_list("schema_migrations")
7
-  (1.5ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
8
-  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
9
- Migrating to CreateUsers (20121003103906)
10
-  (0.0ms) begin transaction
11
-  (0.7ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
12
-  (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES ('20121003103906')
13
-  (1.6ms) commit transaction
14
- Migrating to CreateComments (20121003103958)
15
-  (0.1ms) begin transaction
16
-  (0.3ms) CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "user_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
17
-  (0.0ms) PRAGMA index_list("comments")
18
-  (0.1ms) CREATE INDEX "index_comments_on_user_id" ON "comments" ("user_id")
19
-  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20121003103958')
20
-  (1.4ms) commit transaction
21
-  (0.2ms) select sqlite_version(*)
22
-  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
23
-  (0.0ms) PRAGMA index_list("comments")
24
-  (0.0ms) PRAGMA index_info('index_comments_on_user_id')
25
-  (0.0ms) PRAGMA index_list("users")