gcountry 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/gcountry.rb +84 -25
  2. metadata +1 -1
data/lib/gcountry.rb CHANGED
@@ -1,28 +1,87 @@
1
- module Gcountry
2
- class Country
3
- def get_all_with_pagination(page, role, model)
4
- @page = page
5
- @role = role
6
- @model = model
7
- if !@role || @role == "all"
8
- if @role == "all"
9
- @option = "All"
10
- else
11
- @option = "Select Filter"
12
- end
13
- @data = @model.desc(:created_at).page(@page).per(10)
14
- else
15
- if @role == "0"
16
- @option = "Published"
17
- elsif @role == "1"
18
- @option = "Unpublished"
19
- else
20
- @option = "Trashed"
21
-
22
- end
23
- @data = @model.where(:status => @role).desc(:created_at).page(@page).per(10)
24
- end
25
- return @data, @option
1
+ module ActionView
2
+ module Helpers
3
+ module FormOptionsHelper
4
+ # Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
5
+ def country_select(object, method, priority_countries = nil, options = {}, html_options = {})
6
+ InstanceTag.new(object, method, self, options.delete(:object)).to_country_select_tag(priority_countries, options, html_options)
7
+ end
8
+ # Returns a string of option tags for pretty much any country in the world. Supply a country name as +selected+ to
9
+ # have it marked as the selected option tag. You can also supply an array of countries as +priority_countries+, so
10
+ # that they will be listed above the rest of the (long) list.
11
+ #
12
+ # NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
13
+ def country_options_for_select(selected = nil, priority_countries = nil)
14
+ country_options = ""
15
+
16
+ if priority_countries
17
+ country_options += options_for_select(priority_countries, selected)
18
+ country_options += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
19
+ # prevents selected from being included twice in the HTML which causes
20
+ # some browsers to select the second selected option (not priority)
21
+ # which makes it harder to select an alternative priority country
22
+ selected=nil if priority_countries.include?(selected)
23
+ end
24
+
25
+ return country_options + options_for_select(COUNTRIES, selected)
26
+ end
27
+ # All the countries included in the country_options output.
28
+ COUNTRIES = ["Afghanistan", "Aland Islands", "Albania", "Algeria", "American Samoa", "Andorra", "Angola",
29
+ "Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria",
30
+ "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
31
+ "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil",
32
+ "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia",
33
+ "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China",
34
+ "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo",
35
+ "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba",
36
+ "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt",
37
+ "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)",
38
+ "Faroe Islands", "Fiji", "Finland", "France", "French Guiana", "French Polynesia",
39
+ "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guernsey", "Guinea",
40
+ "Guinea-Bissau", "Guyana", "Haiti", "Heard and McDonald Islands", "Holy See (Vatican City State)",
41
+ "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran, Islamic Republic of", "Iraq",
42
+ "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya",
43
+ "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan",
44
+ "Lao People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya",
45
+ "Liechtenstein", "Lithuania", "Luxembourg", "Macao", "Macedonia, The Former Yugoslav Republic Of",
46
+ "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique",
47
+ "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of",
48
+ "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru",
49
+ "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger",
50
+ "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau",
51
+ "Palestinian Territory, Occupied", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines",
52
+ "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation",
53
+ "Rwanda", "Saint Barthelemy", "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia",
54
+ "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines", "Samoa", "San Marino",
55
+ "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore",
56
+ "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa",
57
+ "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "Sudan", "Suriname",
58
+ "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic",
59
+ "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Timor-Leste",
60
+ "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
61
+ "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom",
62
+ "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela",
63
+ "Viet Nam", "Virgin Islands, British", "Virgin Islands, U.S.", "Wallis and Futuna", "Western Sahara",
64
+ "Yemen", "Zambia", "Zimbabwe"] unless const_defined?("COUNTRIES")
65
+ end
66
+
67
+ class InstanceTag
68
+ def to_country_select_tag(priority_countries, options, html_options)
69
+ html_options = html_options.stringify_keys
70
+ add_default_name_and_id(html_options)
71
+ value = value(object)
72
+ content_tag("select",
73
+ add_options(
74
+ country_options_for_select(value, priority_countries),
75
+ options, value
76
+ ), html_options
77
+ )
78
+ end
79
+ end
80
+
81
+ class FormBuilder
82
+ def country_select(method, priority_countries = nil, options = {}, html_options = {})
83
+ @template.country_select(@object_name, method, priority_countries, options.merge(:object => @object), html_options)
84
+ end
26
85
  end
27
86
  end
28
87
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcountry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: