listly 0.1.2 → 0.1.3

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: 8473d8c7b73747ae59f3b19810ac13c6581599b1
4
- data.tar.gz: d7626d1d04b20f58b6e92ec3be74b33d2adea197
3
+ metadata.gz: 260001fcbec50c2260f25e34237d86b4549e8ec5
4
+ data.tar.gz: 494bdb187588c04fc29cc9ee0214797f5778ad31
5
5
  SHA512:
6
- metadata.gz: 3c1a23f6267745dcf22ea072738fe90e76f733b04629bda641a768ccec648abe9fc29a0ac601624cc2366b384cbb76dbb85cd5bd3fd604a80b9cc3c95c976879
7
- data.tar.gz: 386160249e032d4762b72f87c4ca22fb4ef2f46b528fce169beac46f85bdd792dfbdacdc7edb1dd634cf3f20c0774dde11b0b72d44f59f02dd701438299431f1
6
+ metadata.gz: 54f6b6cb9631ec143ec31f5c76db9a9498461817fb34705d902f11360e8046078d5f009f97025b11bc02758a7e56086036fbcfb36290cc1c05371c3369bc649f
7
+ data.tar.gz: b028e0e30cb6bafadf8571ee93853426ac2efdd5f17e74302ae7267eea7cb0bfa05ac3182cdf2d1021a88bfec41f25ca638e337b36646631faa2e9d1bcc37d26
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- listly (0.1.2)
4
+ listly (0.1.3)
5
5
  rails (>= 4.1.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -26,9 +26,103 @@ Or install it yourself as:
26
26
 
27
27
  $ gem install listly
28
28
 
29
+ ## Configuration
30
+
31
+ Firstly in your rails config/application.rb - you need to set the two config variables
32
+ for the listly gem. The :local_list_store is for the next release. It doesn't do anything just yet.
33
+ The :local_list_constants, is a symbol of a simple module that needs to be loaded in the rails
34
+ load process i.e. putting it in the lib folder will suffice. The module (see below) just needs
35
+ to contain a list of constants and their respective values that relate to where the list data
36
+ is stored. The default is to store this data in the i18n internatialisation files in
37
+ config/locales. Anywhere will do since they all will be loaded!
38
+
39
+
40
+ ```ruby
41
+
42
+ module YourRailsApp
43
+ class Application < Rails::Application
44
+ ...
45
+ config.listly.listly_store_location = :local_list_store
46
+ config.listly.listly_constants_module = :local_list_constants
47
+ ...
48
+ end
49
+ end
50
+
51
+ ```
52
+
53
+ Here is an example of the module and some sample constants that will be turned into lists.
54
+
55
+ ```ruby
56
+
57
+ module LocalListConstants
58
+
59
+ STATE_TYPES = :state_type_hash
60
+ SEX_TYPES = :sex_type_hash
61
+
62
+ end
63
+
64
+ ```
65
+
66
+ The in the "config/locales/views/en.yml" (this can be in any of the locale files) you can
67
+ put the respective data relating to the above constant values.
68
+
69
+ ```ruby
70
+
71
+ states_hash: [
72
+ {code: 'act', name: 'ACT', desc: 'Australian Capital Teritory'},
73
+ {code: 'nsw', name: 'NSW', desc: 'New South Wales'},
74
+ {code: 'nt', name: 'NT', desc: 'Northern Teritory'},
75
+ {code: 'qld', name: 'QLD', desc: 'Queensland'},
76
+ ]
77
+ sex_types_hash: [
78
+ {code: 'male', name: 'Male'},
79
+ {code: 'female', name: 'Female'},
80
+ {code: 'notset', name: 'Not Set'}
81
+ ]
82
+
83
+ ```
84
+
85
+ Listly will create a module from each constant with its respective data. It will create
86
+ an internal class that holds instance properties and attr_reader access methods for each.
87
+ i.e. "states_hash: code" will be the code attribute on the list etc..
88
+
29
89
  ## Usage
30
90
 
31
- TODO: Write usage instructions here
91
+ To therefore use this in a view template in rails or a form - you simply need to include
92
+ the module with the list into the view template. I frequently use mustache view wrappers
93
+ which would look like this:
94
+
95
+ ```ruby
96
+
97
+ require "mustache_extras"
98
+
99
+ module Wrapper
100
+ module Person
101
+
102
+ class New < ::Stache::Mustache::View
103
+ include PageHeader
104
+ include Mustache::FormBuilder
105
+ include Wrapper::Person::Form
106
+ include SexTypeList # <------ This is the included list module!
107
+
108
+ ```
109
+
110
+ Then in the actual form... this is how you can use the list - i.e. the method
111
+ "all_sex_types" was derived from the "SexType" module name and the "sex_type_code"
112
+ and "sex_type_name" methods will populate the form with the relevant "code" and
113
+ "name" data - that was pulled from your i18n locale file!
114
+
115
+
116
+ ```ruby
117
+
118
+ sex_field: f.collection_select(:sex, all_sex_types, :sex_type_code, :sex_type_name, {
119
+ label: t('patients.sex'),
120
+ config: {
121
+ prompt: t('form.please_select'),
122
+ }
123
+ }),
124
+
125
+ ```
32
126
 
33
127
  ## Development
34
128
 
@@ -1,3 +1,3 @@
1
1
  module Listly
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: listly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Forkin
@@ -156,7 +156,6 @@ files:
156
156
  - bin/console
157
157
  - bin/setup
158
158
  - lib/listly.rb
159
- - lib/listly/backup_lists.rb
160
159
  - lib/listly/base_list.rb
161
160
  - lib/listly/list_constants.rb
162
161
  - lib/listly/lists.rb
@@ -1,110 +0,0 @@
1
- #
2
- module ListParent
3
- #
4
- # - creates items of the passed in item_class with the passed in type_prefix for code and name for list items
5
- def all_list_items(type_prefix, item_class, list_items_hash = {})
6
- list_items = []
7
- list_items_hash.each do |item|
8
- data = {}
9
- item.each{ |key, value| data["#{type_prefix}_#{key}"] = "#{value}" }
10
- list_items << Module.const_get("Lists::#{item_class}").new(data)
11
- end
12
- list_items
13
- end
14
-
15
- def list_item_name(all_items, code_field, name_field, list_item_code)
16
- name = ''
17
- all_items.map do |item|
18
- if (item.send code_field) == list_item_code
19
- name = item.send name_field
20
- end
21
- end
22
- name
23
- end
24
-
25
- class MyListType
26
- attr_accessor :list_hash # - this is for convenience methods in Rails
27
-
28
- # - dynamically create the properties from the passed in hash at runtime
29
- define_method :initialize do |args|
30
- @list_hash = args
31
-
32
- args.each do |name, value|
33
- instance_variable_get("@#{name}")
34
- instance_variable_set("@#{name}", value)
35
- end
36
- end
37
-
38
- # - these are for convenience methods in Rails i.e. make lists behave like AR objects
39
- def [](key)
40
- list_hash[key.to_s]
41
- end
42
-
43
- def []=(key, value)
44
- list_hash[key.to_s] = value
45
- end
46
- end
47
-
48
- end
49
- #
50
- module Lists
51
- #
52
- # List Class Names as Constants
53
- TEST1_TYPE = :test1_types_hash
54
- TEST2_TYPE = :test2_types_hash
55
- #
56
-
57
- # some convenience methods to create the list modules and class from the CONSTANT
58
- class << self
59
- # Returns a hash of class names to hash storage details as sym.
60
- def list_name_constants
61
- self.constants.each_with_object({}) do |name, hash|
62
- # Ignore any class constants
63
- next if (storage_location = Lists.const_get(name)).is_a?(Module)
64
- hash[name] = storage_location
65
- end
66
- end
67
-
68
- # Returns a module name from a constant name.
69
- def module_name_for_list_name(name)
70
- module_name = name.to_s.titleize.gsub(' ', '')
71
- end
72
-
73
- def class_name_for_item_name(name)
74
- class_name = name.to_s.titleize.gsub(' ', '')
75
- end
76
- end
77
- end
78
- #
79
- Lists.list_name_constants.each do |name, store_hash|
80
- list_module = Module.new
81
- new_module_name = Lists.module_name_for_list_name(name)
82
- # - add the new module to the list of constants!
83
- list_module.module_eval do
84
- include ListParent
85
-
86
- define_method(:storage_location) { store_hash }
87
-
88
- define_method("all_#{name.to_s.underscore.pluralize}") {
89
- all_list_items(name.to_s.underscore.to_sym,
90
- "#{new_module_name}::My#{new_module_name}", I18n.t("#{store_hash}"))
91
- }
92
-
93
- define_method("#{name.to_s.underscore}_name") {
94
- list_item_name(Module.const_get("all_#{name.to_s.underscore.pluralize}"),
95
- "#{name.to_s.underscore}_code".to_sym, "#{name.to_s.underscore}_name".to_sym,
96
- list_item_code)
97
- }
98
- end
99
- # - create the internal class that will hold the list data (hash)
100
- klass = Class.new(ListParent::MyListType)
101
-
102
- klass.send(:define_method, :attr_accessor) { "#{new_module_name}_code".to_sym }
103
- klass.send(:define_method, :attr_accessor) { "#{new_module_name}_name".to_sym }
104
- klass.send(:define_method, :code) { code }
105
-
106
- # - add this class to the module constants
107
- class_name = "My#{new_module_name}"
108
- list_module.const_set(class_name, klass)
109
- Lists.const_set(new_module_name, list_module)
110
- end